/
Option.php
289 lines (263 loc) · 9.57 KB
/
Option.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Bundle
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Bundle option renderer
*
* @category Mage
* @package Mage_Bundle
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Bundle_Block_Catalog_Product_View_Type_Bundle_Option extends Mage_Bundle_Block_Catalog_Product_Price
{
/**
* Store preconfigured options
*
* @var int|array|string
*/
protected $_selectedOptions = null;
/**
* Show if option has a single selection
*
* @var bool
*/
protected $_showSingle = null;
/**
* Check if option has a single selection
*
* @return bool
*/
protected function _showSingle()
{
if (is_null($this->_showSingle)) {
$_option = $this->getOption();
$_selections = $_option->getSelections();
$this->_showSingle = (count($_selections) == 1 && $_option->getRequired());
}
return $this->_showSingle;
}
/**
* Retrieve default values for template
*
* @return array
*/
protected function _getDefaultValues()
{
$_option = $this->getOption();
$_default = $_option->getDefaultSelection();
$_selections = $_option->getSelections();
$selectedOptions = $this->_getSelectedOptions();
$inPreConfigured = $this->getProduct()->hasPreconfiguredValues()
&& $this->getProduct()->getPreconfiguredValues()
->getData('bundle_option_qty/' . $_option->getId());
if (empty($selectedOptions) && $_default) {
$_defaultQty = $_default->getSelectionQty() * 1;
$_canChangeQty = $_default->getSelectionCanChangeQty();
} elseif (!$inPreConfigured && $selectedOptions && is_numeric($selectedOptions)) {
$selectedSelection = $_option->getSelectionById($selectedOptions);
if ($selectedSelection) {
$_defaultQty = $selectedSelection->getSelectionQty() * 1;
$_canChangeQty = $selectedSelection->getSelectionCanChangeQty();
} else {
$_defaultQty = $_selections[0]->getSelectionQty() * 1;
$_canChangeQty = $_selections[0]->getSelectionCanChangeQty();
}
} elseif (!$this->_showSingle() || $inPreConfigured) {
$_defaultQty = $this->_getSelectedQty();
$_canChangeQty = (bool)$_defaultQty;
} else {
$_defaultQty = $_selections[0]->getSelectionQty() * 1;
$_canChangeQty = $_selections[0]->getSelectionCanChangeQty();
}
return array($_defaultQty, $_canChangeQty);
}
/**
* Collect selected options
*
* @return void
*/
protected function _getSelectedOptions()
{
if (is_null($this->_selectedOptions)) {
$this->_selectedOptions = array();
$option = $this->getOption();
if ($this->getProduct()->hasPreconfiguredValues()) {
$configValue = $this->getProduct()->getPreconfiguredValues()
->getData('bundle_option/' . $option->getId());
if ($configValue) {
$this->_selectedOptions = $configValue;
} elseif (!$option->getRequired()) {
$this->_selectedOptions = 'None';
}
}
}
return $this->_selectedOptions;
}
/**
* Define if selection is selected
*
* @param Mage_Catalog_Model_Product $selection
* @return bool
*/
protected function _isSelected($selection)
{
$selectedOptions = $this->_getSelectedOptions();
if (is_numeric($selectedOptions)) {
return ($selection->getSelectionId() == $this->_getSelectedOptions());
} elseif (is_array($selectedOptions) && !empty($selectedOptions)) {
return in_array($selection->getSelectionId(), $this->_getSelectedOptions());
} elseif ($selectedOptions == 'None') {
return false;
} else {
return ($selection->getIsDefault() && $selection->isSaleable());
}
}
/**
* Retrieve selected option qty
*
* @return int
*/
protected function _getSelectedQty()
{
if ($this->getProduct()->hasPreconfiguredValues()) {
$selectedQty = (float)$this->getProduct()->getPreconfiguredValues()
->getData('bundle_option_qty/' . $this->getOption()->getId());
if ($selectedQty < 0) {
$selectedQty = 0;
}
} else {
$selectedQty = 0;
}
return $selectedQty;
}
/**
* Get product model
*
* @return Mage_Catalog_Model_Product
*/
public function getProduct()
{
if (!$this->hasData('product')) {
$this->setData('product', Mage::registry('current_product'));
}
return $this->getData('product');
}
/**
* Returns the formatted string for the quantity chosen for the given selection
*
* @param Mage_Catalog_Model_Proudct $_selection
* @param bool $includeContainer
* @return string
*/
public function getSelectionQtyTitlePrice($_selection, $includeContainer = true)
{
$price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
$this->setFormatProduct($_selection);
$priceTitle = $_selection->getSelectionQty() * 1 . ' x ' . $this->escapeHtml($_selection->getName());
$priceTitle .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
. '+' . $this->formatPriceString($price, $includeContainer)
. ($includeContainer ? '</span>' : '');
return $priceTitle;
}
/**
* Get price for selection product
*
* @param Mage_Catalog_Model_Product $_selection
* @return int|float
*/
public function getSelectionPrice($_selection)
{
$price = 0;
$store = $this->getProduct()->getStore();
if ($_selection) {
$price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection);
if (is_numeric($price)) {
$price = $this->helper('core')->currencyByStore($price, $store, false);
}
}
return is_numeric($price) ? $price : 0;
}
/**
* Get title price for selection product
*
* @param Mage_Catalog_Model_Product $_selection
* @param bool $includeContainer
* @return string
*/
public function getSelectionTitlePrice($_selection, $includeContainer = true)
{
$price = $this->getProduct()->getPriceModel()->getSelectionPreFinalPrice($this->getProduct(), $_selection, 1);
$this->setFormatProduct($_selection);
$priceTitle = $this->escapeHtml($_selection->getName());
$priceTitle .= ' ' . ($includeContainer ? '<span class="price-notice">' : '')
. '+' . $this->formatPriceString($price, $includeContainer)
. ($includeContainer ? '</span>' : '');
return $priceTitle;
}
/**
* Set JS validation container for element
*
* @param int $elementId
* @param int $containerId
* @return string
*/
public function setValidationContainer($elementId, $containerId)
{
return '<script type="text/javascript">
$(\'' . $elementId . '\').advaiceContainer = \'' . $containerId . '\';
$(\'' . $elementId . '\').callbackFunction = \'bundle.validationCallback\';
</script>';
}
/**
* Format price string
*
* @param float $price
* @param bool $includeContainer
* @return string
*/
public function formatPriceString($price, $includeContainer = true)
{
$taxHelper = Mage::helper('tax');
$coreHelper = $this->helper('core');
$currentProduct = $this->getProduct();
if ($currentProduct->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC
&& $this->getFormatProduct()
) {
$product = $this->getFormatProduct();
} else {
$product = $currentProduct;
}
$priceTax = $taxHelper->getPrice($product, $price);
$priceIncTax = $taxHelper->getPrice($product, $price, true);
$formated = $coreHelper->currencyByStore($priceTax, $product->getStore(), true, $includeContainer);
if ($taxHelper->displayBothPrices() && $priceTax != $priceIncTax) {
$formated .=
' (+' .
$coreHelper->currencyByStore($priceIncTax, $product->getStore(), true, $includeContainer) .
' ' . $this->__('Incl. Tax') . ')';
}
return $formated;
}
}