/
Configuration.php
275 lines (253 loc) · 10.2 KB
/
Configuration.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
<?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_Catalog
* @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)
*/
/**
* Helper for fetching properties by product configurational item
*
* @category Mage
* @package Mage_Catalog
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Catalog_Helper_Product_Configuration extends Mage_Core_Helper_Abstract implements Mage_Catalog_Helper_Product_Configuration_Interface
{
const XML_PATH_CONFIGURABLE_ALLOWED_TYPES = 'global/catalog/product/type/configurable/allow_product_types';
/**
* Retrieves product configuration options
*
* @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
* @return array
*/
public function getCustomOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
{
$product = $item->getProduct();
$options = array();
$optionIds = $item->getOptionByCode('option_ids');
if ($optionIds) {
$options = array();
foreach (explode(',', $optionIds->getValue()) as $optionId) {
$option = $product->getOptionById($optionId);
if ($option) {
$itemOption = $item->getOptionByCode('option_' . $option->getId());
$group = $option->groupFactory($option->getType())
->setOption($option)
->setConfigurationItem($item)
->setConfigurationItemOption($itemOption);
if ('file' == $option->getType()) {
$downloadParams = $item->getFileDownloadParams();
if ($downloadParams) {
$url = $downloadParams->getUrl();
if ($url) {
$group->setCustomOptionDownloadUrl($url);
}
$urlParams = $downloadParams->getUrlParams();
if ($urlParams) {
$group->setCustomOptionUrlParams($urlParams);
}
}
}
$options[] = array(
'label' => $option->getTitle(),
'value' => $group->getFormattedOptionValue($itemOption->getValue()),
'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
'option_id' => $option->getId(),
'option_type' => $option->getType(),
'custom_view' => $group->isCustomizedView()
);
}
}
}
$addOptions = $item->getOptionByCode('additional_options');
if ($addOptions) {
$options = array_merge($options, unserialize($addOptions->getValue(), ['allowed_classes' => false]));
}
return $options;
}
/**
* Retrieves configuration options for configurable product
*
* @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
* @return array
*/
public function getConfigurableOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
{
$product = $item->getProduct();
$typeId = $product->getTypeId();
if ($typeId != Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
Mage::throwException($this->__('Wrong product type to extract configurable options.'));
}
$attributes = $product->getTypeInstance(true)
->getSelectedAttributesInfo($product);
return array_merge($attributes, $this->getCustomOptions($item));
}
/**
* Retrieves configuration options for grouped product
*
* @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
* @return array
*/
public function getGroupedOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
{
$product = $item->getProduct();
$typeId = $product->getTypeId();
if ($typeId != Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
Mage::throwException($this->__('Wrong product type to extract configurable options.'));
}
$options = array();
/**
* @var Mage_Catalog_Model_Product_Type_Grouped
*/
$typeInstance = $product->getTypeInstance(true);
$associatedProducts = $typeInstance->getAssociatedProducts($product);
if ($associatedProducts) {
foreach ($associatedProducts as $associatedProduct) {
$qty = $item->getOptionByCode('associated_product_' . $associatedProduct->getId());
$option = array(
'label' => $associatedProduct->getName(),
'value' => ($qty && $qty->getValue()) ? $qty->getValue() : 0
);
$options[] = $option;
}
}
$options = array_merge($options, $this->getCustomOptions($item));
$isUnConfigured = true;
foreach ($options as &$option) {
if ($option['value']) {
$isUnConfigured = false;
break;
}
}
return $isUnConfigured ? array() : $options;
}
/**
* Retrieves product options list
*
* @param Mage_Catalog_Model_Product_Configuration_Item_Interface $item
* @return array
*/
public function getOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
{
$typeId = $item->getProduct()->getTypeId();
switch ($typeId) {
case Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE:
return $this->getConfigurableOptions($item);
break;
case Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE:
return $this->getGroupedOptions($item);
break;
}
return $this->getCustomOptions($item);
}
/**
* Accept option value and return its formatted view
*
* @param mixed $optionValue
* Method works well with these $optionValue format:
* 1. String
* 2. Indexed array e.g. array(val1, val2, ...)
* 3. Associative array, containing additional option info, including option value, e.g.
* array
* (
* [label] => ...,
* [value] => ...,
* [print_value] => ...,
* [option_id] => ...,
* [option_type] => ...,
* [custom_view] =>...,
* )
* @param array $params
* All keys are options. Following supported:
* - 'maxLength': truncate option value if needed, default: do not truncate
* - 'cutReplacer': replacer for cut off value part when option value exceeds maxLength
*
* @return array
*/
public function getFormattedOptionValue($optionValue, $params = null)
{
// Init params
if (!$params) {
$params = array();
}
$maxLength = isset($params['max_length']) ? $params['max_length'] : null;
$cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
// Proceed with option
$optionInfo = array();
// Define input data format
if (is_array($optionValue)) {
if (isset($optionValue['option_id'])) {
$optionInfo = $optionValue;
if (isset($optionInfo['value'])) {
$optionValue = $optionInfo['value'];
}
} elseif (isset($optionValue['value'])) {
$optionValue = $optionValue['value'];
}
}
// Render customized option view
if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
$_default = array('value' => $optionValue);
if (isset($optionInfo['option_type'])) {
try {
$group = Mage::getModel('catalog/product_option')->groupFactory($optionInfo['option_type']);
return array('value' => $group->getCustomizedView($optionInfo));
} catch (Exception $e) {
return $_default;
}
}
return $_default;
}
// Truncate standard view
$result = array();
if (is_array($optionValue)) {
$_truncatedValue = implode("\n", $optionValue);
$_truncatedValue = nl2br($_truncatedValue);
return array('value' => $_truncatedValue);
} else {
if ($maxLength) {
$_truncatedValue = Mage::helper('core/string')->truncate($optionValue, $maxLength, '');
} else {
$_truncatedValue = $optionValue;
}
$_truncatedValue = nl2br($_truncatedValue);
}
$result = array('value' => $_truncatedValue);
if ($maxLength && (Mage::helper('core/string')->strlen($optionValue) > $maxLength)) {
$result['value'] = $result['value'] . $cutReplacer;
$optionValue = nl2br($optionValue);
$result['full_view'] = $optionValue;
}
return $result;
}
/**
* Get allowed product types for configurable product
*
* @return SimpleXMLElement
*/
public function getConfigurableAllowedTypes()
{
return Mage::getConfig()
->getNode(self::XML_PATH_CONFIGURABLE_ALLOWED_TYPES)
->children();
}
}