00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Catalog_Block_Product_View_Options extends Mage_Core_Block_Template
00036 {
00037 protected $_product;
00038
00039 protected $_optionRenders = array();
00040
00041 public function __construct()
00042 {
00043 parent::__construct();
00044 $this->addOptionRenderer(
00045 'default',
00046 'catalog/product_view_options_type_default',
00047 'catalog/product/view/options/type/default.phtml'
00048 );
00049 }
00050
00051
00052
00053
00054
00055
00056 public function getProduct()
00057 {
00058 if (!$this->_product) {
00059 if (Mage::registry('product')) {
00060 $this->_product = Mage::registry('current_product');
00061 } else {
00062 $this->_product = Mage::getSingleton('catalog/product');
00063 }
00064 }
00065 return $this->_product;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 public function setProduct(Mage_Catalog_Model_Product $product = null)
00075 {
00076 $this->_product = $product;
00077 return $this;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 public function addOptionRenderer($type, $block, $template)
00089 {
00090 $this->_optionRenders[$type] = array(
00091 'block' => $block,
00092 'template' => $template,
00093 'renderer' => null
00094 );
00095 return $this;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104 public function getOptionRender($type)
00105 {
00106 if (isset($this->_optionRenders[$type])) {
00107 return $this->_optionRenders[$type];
00108 }
00109
00110 return $this->_optionRenders['default'];
00111 }
00112
00113 public function getGroupOfOption($type)
00114 {
00115 $group = Mage::getSingleton('catalog/product_option')->getGroupByType($type);
00116
00117 return $group == '' ? 'default' : $group;
00118 }
00119
00120
00121
00122
00123
00124
00125 public function getOptions()
00126 {
00127 return $this->getProduct()->getOptions();
00128 }
00129
00130 public function hasOptions()
00131 {
00132 if ($this->getOptions()) {
00133 return true;
00134 }
00135 return false;
00136 }
00137
00138 public function getJsonConfig()
00139 {
00140 $config = array();
00141
00142 foreach ($this->getOptions() as $option) {
00143
00144 $priceValue = 0;
00145 if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
00146 $_tmpPriceValues = array();
00147 foreach ($option->getValues() as $value) {
00148
00149 $_tmpPriceValues[$value->getId()] = Mage::helper('core')->currency($value->getPrice(true), false, false);
00150 }
00151 $priceValue = $_tmpPriceValues;
00152 } else {
00153 $priceValue = Mage::helper('core')->currency($option->getPrice(), false, false);
00154 }
00155 $config[$option->getId()] = $priceValue;
00156 }
00157
00158 return Zend_Json::encode($config);
00159 }
00160
00161
00162
00163
00164
00165
00166 public function getOptionHtml(Mage_Catalog_Model_Product_Option $option)
00167 {
00168 $renderer = $this->getOptionRender(
00169 $this->getGroupOfOption($option->getType())
00170 );
00171 if (is_null($renderer['renderer'])) {
00172 $renderer['renderer'] = $this->getLayout()->createBlock($renderer['block'])
00173 ->setTemplate($renderer['template']);
00174 }
00175 return $renderer['renderer']
00176 ->setProduct($this->getProduct())
00177 ->setOption($option)
00178 ->toHtml();
00179 }
00180 }