1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Block_Product_View_Options extends Mage_Core_Block_Template
36: {
37: protected $_product;
38:
39: protected $_optionRenders = array();
40:
41: public function __construct()
42: {
43: parent::__construct();
44: $this->addOptionRenderer(
45: 'default',
46: 'catalog/product_view_options_type_default',
47: 'catalog/product/view/options/type/default.phtml'
48: );
49: }
50:
51: 52: 53: 54: 55:
56: public function getProduct()
57: {
58: if (!$this->_product) {
59: if (Mage::registry('current_product')) {
60: $this->_product = Mage::registry('current_product');
61: } else {
62: $this->_product = Mage::getSingleton('catalog/product');
63: }
64: }
65: return $this->_product;
66: }
67:
68: 69: 70: 71: 72: 73:
74: public function setProduct(Mage_Catalog_Model_Product $product = null)
75: {
76: $this->_product = $product;
77: return $this;
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: public function addOptionRenderer($type, $block, $template)
89: {
90: $this->_optionRenders[$type] = array(
91: 'block' => $block,
92: 'template' => $template,
93: 'renderer' => null
94: );
95: return $this;
96: }
97:
98: 99: 100: 101: 102: 103:
104: public function getOptionRender($type)
105: {
106: if (isset($this->_optionRenders[$type])) {
107: return $this->_optionRenders[$type];
108: }
109:
110: return $this->_optionRenders['default'];
111: }
112:
113: public function getGroupOfOption($type)
114: {
115: $group = Mage::getSingleton('catalog/product_option')->getGroupByType($type);
116:
117: return $group == '' ? 'default' : $group;
118: }
119:
120: 121: 122: 123: 124:
125: public function getOptions()
126: {
127: return $this->getProduct()->getOptions();
128: }
129:
130: public function hasOptions()
131: {
132: if ($this->getOptions()) {
133: return true;
134: }
135: return false;
136: }
137:
138: 139: 140: 141: 142: 143:
144: protected function _getPriceConfiguration($option)
145: {
146: $data = array();
147: $data['price'] = Mage::helper('core')->currency($option->getPrice(true), false, false);
148: $data['oldPrice'] = Mage::helper('core')->currency($option->getPrice(false), false, false);
149: $data['priceValue'] = $option->getPrice(false);
150: $data['type'] = $option->getPriceType();
151: $data['excludeTax'] = $price = Mage::helper('tax')->getPrice($option->getProduct(), $data['price'], false);
152: $data['includeTax'] = $price = Mage::helper('tax')->getPrice($option->getProduct(), $data['price'], true);
153: return $data;
154: }
155:
156: 157: 158: 159: 160:
161: public function getJsonConfig()
162: {
163: $config = array();
164:
165: foreach ($this->getOptions() as $option) {
166:
167: $priceValue = 0;
168: if ($option->getGroupByType() == Mage_Catalog_Model_Product_Option::OPTION_GROUP_SELECT) {
169: $_tmpPriceValues = array();
170: foreach ($option->getValues() as $value) {
171:
172: $id = $value->getId();
173: $_tmpPriceValues[$id] = $this->_getPriceConfiguration($value);
174: }
175: $priceValue = $_tmpPriceValues;
176: } else {
177: $priceValue = $this->_getPriceConfiguration($option);
178: }
179: $config[$option->getId()] = $priceValue;
180: }
181:
182: return Mage::helper('core')->jsonEncode($config);
183: }
184:
185: 186: 187: 188: 189:
190: public function getOptionHtml(Mage_Catalog_Model_Product_Option $option)
191: {
192: $renderer = $this->getOptionRender(
193: $this->getGroupOfOption($option->getType())
194: );
195: if (is_null($renderer['renderer'])) {
196: $renderer['renderer'] = $this->getLayout()->createBlock($renderer['block'])
197: ->setTemplate($renderer['template']);
198: }
199: return $renderer['renderer']
200: ->setProduct($this->getProduct())
201: ->setOption($option)
202: ->toHtml();
203: }
204: }
205: