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: class Mage_Catalog_Helper_Product_Configuration extends Mage_Core_Helper_Abstract
35: implements Mage_Catalog_Helper_Product_Configuration_Interface
36: {
37: const XML_PATH_CONFIGURABLE_ALLOWED_TYPES = 'global/catalog/product/type/configurable/allow_product_types';
38:
39: 40: 41: 42: 43: 44:
45: public function getCustomOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
46: {
47: $product = $item->getProduct();
48: $options = array();
49: $optionIds = $item->getOptionByCode('option_ids');
50: if ($optionIds) {
51: $options = array();
52: foreach (explode(',', $optionIds->getValue()) as $optionId) {
53: $option = $product->getOptionById($optionId);
54: if ($option) {
55: $itemOption = $item->getOptionByCode('option_' . $option->getId());
56: $group = $option->groupFactory($option->getType())
57: ->setOption($option)
58: ->setConfigurationItem($item)
59: ->setConfigurationItemOption($itemOption);
60:
61: if ('file' == $option->getType()) {
62: $downloadParams = $item->getFileDownloadParams();
63: if ($downloadParams) {
64: $url = $downloadParams->getUrl();
65: if ($url) {
66: $group->setCustomOptionDownloadUrl($url);
67: }
68: $urlParams = $downloadParams->getUrlParams();
69: if ($urlParams) {
70: $group->setCustomOptionUrlParams($urlParams);
71: }
72: }
73: }
74:
75: $options[] = array(
76: 'label' => $option->getTitle(),
77: 'value' => $group->getFormattedOptionValue($itemOption->getValue()),
78: 'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
79: 'option_id' => $option->getId(),
80: 'option_type' => $option->getType(),
81: 'custom_view' => $group->isCustomizedView()
82: );
83: }
84: }
85: }
86:
87: $addOptions = $item->getOptionByCode('additional_options');
88: if ($addOptions) {
89: $options = array_merge($options, unserialize($addOptions->getValue()));
90: }
91:
92: return $options;
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function getConfigurableOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
102: {
103: $product = $item->getProduct();
104: $typeId = $product->getTypeId();
105: if ($typeId != Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
106: Mage::throwException($this->__('Wrong product type to extract configurable options.'));
107: }
108: $attributes = $product->getTypeInstance(true)
109: ->getSelectedAttributesInfo($product);
110: return array_merge($attributes, $this->getCustomOptions($item));
111: }
112:
113: 114: 115: 116: 117: 118:
119: public function getGroupedOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
120: {
121: $product = $item->getProduct();
122: $typeId = $product->getTypeId();
123: if ($typeId != Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE) {
124: Mage::throwException($this->__('Wrong product type to extract configurable options.'));
125: }
126:
127: $options = array();
128: 129: 130:
131: $typeInstance = $product->getTypeInstance(true);
132: $associatedProducts = $typeInstance->getAssociatedProducts($product);
133:
134: if ($associatedProducts) {
135: foreach ($associatedProducts as $associatedProduct) {
136: $qty = $item->getOptionByCode('associated_product_' . $associatedProduct->getId());
137: $option = array(
138: 'label' => $associatedProduct->getName(),
139: 'value' => ($qty && $qty->getValue()) ? $qty->getValue() : 0
140: );
141:
142: $options[] = $option;
143: }
144: }
145:
146: $options = array_merge($options, $this->getCustomOptions($item));
147: $isUnConfigured = true;
148: foreach ($options as &$option) {
149: if ($option['value']) {
150: $isUnConfigured = false;
151: break;
152: }
153: }
154: return $isUnConfigured ? array() : $options;
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function getOptions(Mage_Catalog_Model_Product_Configuration_Item_Interface $item)
164: {
165: $typeId = $item->getProduct()->getTypeId();
166: switch ($typeId) {
167: case Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE:
168: return $this->getConfigurableOptions($item);
169: break;
170: case Mage_Catalog_Model_Product_Type_Grouped::TYPE_CODE:
171: return $this->getGroupedOptions($item);
172: break;
173: }
174: return $this->getCustomOptions($item);
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: public function getFormattedOptionValue($optionValue, $params = null)
202: {
203:
204: if (!$params) {
205: $params = array();
206: }
207: $maxLength = isset($params['max_length']) ? $params['max_length'] : null;
208: $cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
209:
210:
211: $optionInfo = array();
212:
213:
214: if (is_array($optionValue)) {
215: if (isset($optionValue['option_id'])) {
216: $optionInfo = $optionValue;
217: if (isset($optionInfo['value'])) {
218: $optionValue = $optionInfo['value'];
219: }
220: } else if (isset($optionValue['value'])) {
221: $optionValue = $optionValue['value'];
222: }
223: }
224:
225:
226: if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
227: $_default = array('value' => $optionValue);
228: if (isset($optionInfo['option_type'])) {
229: try {
230: $group = Mage::getModel('catalog/product_option')->groupFactory($optionInfo['option_type']);
231: return array('value' => $group->getCustomizedView($optionInfo));
232: } catch (Exception $e) {
233: return $_default;
234: }
235: }
236: return $_default;
237: }
238:
239:
240: $result = array();
241: if (is_array($optionValue)) {
242: $_truncatedValue = implode("\n", $optionValue);
243: $_truncatedValue = nl2br($_truncatedValue);
244: return array('value' => $_truncatedValue);
245: } else {
246: if ($maxLength) {
247: $_truncatedValue = Mage::helper('core/string')->truncate($optionValue, $maxLength, '');
248: } else {
249: $_truncatedValue = $optionValue;
250: }
251: $_truncatedValue = nl2br($_truncatedValue);
252: }
253:
254: $result = array('value' => $_truncatedValue);
255:
256: if ($maxLength && (Mage::helper('core/string')->strlen($optionValue) > $maxLength)) {
257: $result['value'] = $result['value'] . $cutReplacer;
258: $optionValue = nl2br($optionValue);
259: $result['full_view'] = $optionValue;
260: }
261:
262: return $result;
263: }
264:
265: 266: 267: 268: 269:
270: public function getConfigurableAllowedTypes()
271: {
272: return Mage::getConfig()
273: ->getNode(self::XML_PATH_CONFIGURABLE_ALLOWED_TYPES)
274: ->children();
275: }
276: }
277: