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_Bundle_Block_Catalog_Product_View_Type_Bundle_Option extends Mage_Bundle_Block_Catalog_Product_Price
36: {
37: 38: 39: 40: 41:
42: protected $_selectedOptions = null;
43:
44: 45: 46: 47: 48:
49: protected $_showSingle = null;
50:
51: 52: 53: 54: 55:
56: protected function _showSingle()
57: {
58: if (is_null($this->_showSingle)) {
59: $_option = $this->getOption();
60: $_selections = $_option->getSelections();
61:
62: $this->_showSingle = (count($_selections) == 1 && $_option->getRequired());
63: }
64:
65: return $this->_showSingle;
66: }
67:
68: 69: 70: 71: 72:
73: protected function _getDefaultValues()
74: {
75: $_option = $this->getOption();
76: $_default = $_option->getDefaultSelection();
77: $_selections = $_option->getSelections();
78: $selectedOptions = $this->_getSelectedOptions();
79: $inPreConfigured = $this->getProduct()->hasPreconfiguredValues()
80: && $this->getProduct()->getPreconfiguredValues()
81: ->getData('bundle_option_qty/' . $_option->getId());
82:
83: if (empty($selectedOptions) && $_default) {
84: $_defaultQty = $_default->getSelectionQty()*1;
85: $_canChangeQty = $_default->getSelectionCanChangeQty();
86: } elseif (!$inPreConfigured && $selectedOptions && is_numeric($selectedOptions)) {
87: $selectedSelection = $_option->getSelectionById($selectedOptions);
88: $_defaultQty = $selectedSelection->getSelectionQty()*1;
89: $_canChangeQty = $selectedSelection->getSelectionCanChangeQty();
90: } elseif (!$this->_showSingle() || $inPreConfigured) {
91: $_defaultQty = $this->_getSelectedQty();
92: $_canChangeQty = (bool)$_defaultQty;
93: } else {
94: $_defaultQty = $_selections[0]->getSelectionQty()*1;
95: $_canChangeQty = $_selections[0]->getSelectionCanChangeQty();
96: }
97:
98: return array($_defaultQty, $_canChangeQty);
99: }
100:
101: 102: 103: 104: 105:
106: protected function _getSelectedOptions()
107: {
108: if (is_null($this->_selectedOptions)) {
109: $this->_selectedOptions = array();
110: $option = $this->getOption();
111:
112: if ($this->getProduct()->hasPreconfiguredValues()) {
113: $configValue = $this->getProduct()->getPreconfiguredValues()
114: ->getData('bundle_option/' . $option->getId());
115: if ($configValue) {
116: $this->_selectedOptions = $configValue;
117: } elseif (!$option->getRequired()) {
118: $this->_selectedOptions = 'None';
119: }
120: }
121: }
122:
123: return $this->_selectedOptions;
124: }
125:
126: 127: 128: 129: 130: 131:
132: protected function _isSelected($selection)
133: {
134: $selectedOptions = $this->_getSelectedOptions();
135: if (is_numeric($selectedOptions)) {
136: return ($selection->getSelectionId() == $this->_getSelectedOptions());
137: } elseif (is_array($selectedOptions) && !empty($selectedOptions)) {
138: return in_array($selection->getSelectionId(), $this->_getSelectedOptions());
139: } elseif ($selectedOptions == 'None') {
140: return false;
141: } else {
142: return ($selection->getIsDefault() && $selection->isSaleable());
143: }
144: }
145:
146: 147: 148: 149: 150:
151: protected function _getSelectedQty()
152: {
153: if ($this->getProduct()->hasPreconfiguredValues()) {
154: $selectedQty = (float)$this->getProduct()->getPreconfiguredValues()
155: ->getData('bundle_option_qty/' . $this->getOption()->getId());
156: if ($selectedQty < 0) {
157: $selectedQty = 0;
158: }
159: } else {
160: $selectedQty = 0;
161: }
162:
163: return $selectedQty;
164: }
165:
166: 167: 168: 169: 170:
171: public function getProduct()
172: {
173: if (!$this->hasData('product')) {
174: $this->setData('product', Mage::registry('current_product'));
175: }
176: return $this->getData('product');
177: }
178:
179: public function getSelectionQtyTitlePrice($_selection, $includeContainer = true)
180: {
181: $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
182: $this->setFormatProduct($_selection);
183: $priceTitle = $_selection->getSelectionQty()*1 . ' x ' . $this->escapeHtml($_selection->getName());
184:
185: $priceTitle .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
186: . '+' . $this->formatPriceString($price, $includeContainer)
187: . ($includeContainer ? '</span>' : '');
188:
189: return $priceTitle;
190: }
191:
192: 193: 194: 195: 196: 197:
198: public function getSelectionPrice($_selection)
199: {
200: $price = 0;
201: $store = $this->getProduct()->getStore();
202: if ($_selection) {
203: $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
204: if (is_numeric($price)) {
205: $price = $this->helper('core')->currencyByStore($price, $store, false);
206: }
207: }
208: return is_numeric($price) ? $price : 0;
209: }
210:
211: 212: 213: 214: 215: 216: 217:
218: public function getSelectionTitlePrice($_selection, $includeContainer = true)
219: {
220: $price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection, 1);
221: $this->setFormatProduct($_selection);
222: $priceTitle = $this->escapeHtml($_selection->getName());
223: $priceTitle .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
224: . '+' . $this->formatPriceString($price, $includeContainer)
225: . ($includeContainer ? '</span>' : '');
226: return $priceTitle;
227: }
228:
229: 230: 231: 232: 233: 234: 235:
236: public function setValidationContainer($elementId, $containerId)
237: {
238: return '<script type="text/javascript">
239: $(\'' . $elementId . '\').advaiceContainer = \'' . $containerId . '\';
240: $(\'' . $elementId . '\').callbackFunction = \'bundle.validationCallback\';
241: </script>';
242: }
243:
244: 245: 246: 247: 248: 249: 250:
251: public function formatPriceString($price, $includeContainer = true)
252: {
253: $taxHelper = Mage::helper('tax');
254: $coreHelper = $this->helper('core');
255: $currentProduct = $this->getProduct();
256: if ($currentProduct->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC
257: && $this->getFormatProduct()
258: ) {
259: $product = $this->getFormatProduct();
260: } else {
261: $product = $currentProduct;
262: }
263:
264: $priceTax = $taxHelper->getPrice($product, $price);
265: $priceIncTax = $taxHelper->getPrice($product, $price, true);
266:
267: $formated = $coreHelper->currencyByStore($priceTax, $product->getStore(), true, $includeContainer);
268: if ($taxHelper->displayBothPrices() && $priceTax != $priceIncTax) {
269: $formated .=
270: ' (+' .
271: $coreHelper->currencyByStore($priceIncTax, $product->getStore(), true, $includeContainer) .
272: ' ' . $this->__('Incl. Tax') . ')';
273: }
274:
275: return $formated;
276: }
277: }
278: