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_XmlConnect_Block_Catalog_Product_Options_Configurable extends Mage_XmlConnect_Block_Catalog_Product_Options
35: {
36: 37: 38: 39: 40: 41: 42:
43: public function getProductOptionsXml(Mage_Catalog_Model_Product $product, $isObject = false)
44: {
45: $xmlModel = $this->getProductCustomOptionsXmlObject($product);
46: $optionsXmlObj = $xmlModel->options;
47: $options = array();
48:
49: if (!$product->isSaleable()) {
50: return $isObject ? $xmlModel : $xmlModel->asNiceXml();
51: }
52:
53: 54: 55:
56: $_attributes = $product->getTypeInstance(true)->getConfigurableAttributes($product);
57: if (!sizeof($_attributes)) {
58: return $isObject ? $xmlModel : $xmlModel->asNiceXml();
59: }
60:
61: $_allowProducts = array();
62: $_allProducts = $product->getTypeInstance(true)->getUsedProducts(null, $product);
63: foreach ($_allProducts as $_product) {
64: if ($_product->isSaleable()) {
65: $_allowProducts[] = $_product;
66: }
67: }
68:
69: 70: 71:
72: foreach ($_allowProducts as $_item) {
73: $_productId = $_item->getId();
74:
75: foreach ($_attributes as $attribute) {
76: $productAttribute = $attribute->getProductAttribute();
77: $attributeValue = $_item->getData($productAttribute->getAttributeCode());
78: if (!isset($options[$productAttribute->getId()])) {
79: $options[$productAttribute->getId()] = array();
80: }
81:
82: if (!isset($options[$productAttribute->getId()][$attributeValue])) {
83: $options[$productAttribute->getId()][$attributeValue] = array();
84: }
85: $options[$productAttribute->getId()][$attributeValue][] = $_productId;
86: }
87: }
88:
89: foreach ($_attributes as $attribute) {
90: $productAttribute = $attribute->getProductAttribute();
91: $attributeId = $productAttribute->getId();
92: $info = array(
93: 'id' => $productAttribute->getId(),
94: 'label' => $attribute->getLabel(),
95: 'options' => array()
96: );
97:
98: $prices = $attribute->getPrices();
99: if (is_array($prices)) {
100: foreach ($prices as $value) {
101: if (!isset($options[$attributeId][$value['value_index']])) {
102: continue;
103: }
104: $price = Mage::helper('xmlconnect')->formatPriceForXml(
105: $this->_preparePrice($product, $value['pricing_value'], $value['is_percent'])
106: );
107: $optionProducts = array();
108: if (isset($options[$attributeId][$value['value_index']])) {
109: $optionProducts = $options[$attributeId][$value['value_index']];
110: }
111: $info['options'][] = array(
112: 'id' => $value['value_index'],
113: 'label' => $value['label'],
114: 'price' => $price,
115: 'formated_price' => $this->_formatPriceString($price, $product),
116: 'products' => $optionProducts,
117: );
118: }
119: }
120:
121: if (sizeof($info['options']) > 0) {
122: $attributes[$attributeId] = $info;
123: }
124: }
125:
126: $isFirst = true;
127:
128: $_attributes = $attributes;
129: reset($_attributes);
130: foreach ($attributes as $id => $attribute) {
131: $optionNode = $optionsXmlObj->addChild('option');
132: $optionNode->addAttribute('code', 'super_attribute[' . $id . ']');
133: $optionNode->addAttribute('type', 'select');
134: $optionNode->addAttribute('label', $optionsXmlObj->escapeXml($attribute['label']));
135: $optionNode->addAttribute('is_required', 1);
136: if ($isFirst) {
137: foreach ($attribute['options'] as $option) {
138: $valueNode = $optionNode->addChild('value');
139: $valueNode->addAttribute('code', $option['id']);
140: $valueNode->addAttribute('label', $optionsXmlObj->escapeXml($option['label']));
141: if ((float)$option['price'] != 0.00) {
142: $valueNode->addAttribute('price', $option['price']);
143: $valueNode->addAttribute('formated_price', $option['formated_price']);
144: }
145: if (sizeof($_attributes) > 1) {
146: $this->_prepareRecursivelyRelatedValues($valueNode, $_attributes, $option['products'], 1);
147: }
148: }
149: $isFirst = false;
150: }
151: }
152:
153: return $isObject ? $xmlModel : $xmlModel->asNiceXml();
154: }
155:
156: 157: 158: 159: 160: 161: 162: 163:
164: protected function _prepareRecursivelyRelatedValues(&$valueNode, $attributes, $productIds, $cycle = 1)
165: {
166: $relatedNode = null;
167:
168: for ($i = 0; $i < $cycle; $i++) {
169: next($attributes);
170: }
171: $attribute = current($attributes);
172: $attrId = key($attributes);
173: foreach ($attribute['options'] as $option) {
174: 175: 176:
177: $intersect = array_intersect($productIds, $option['products']);
178:
179: if (empty($intersect)) {
180: continue;
181: }
182:
183: if ($relatedNode === null) {
184: $relatedNode = $valueNode->addChild('relation');
185: $relatedNode->addAttribute('to', 'super_attribute[' . $attrId . ']');
186: }
187:
188: $_valueNode = $relatedNode->addChild('value');
189: $_valueNode->addAttribute('code', $option['id']);
190: $_valueNode->addAttribute('label', $_valueNode->escapeXml($option['label']));
191: if ((float)$option['price'] != 0.00) {
192: $_valueNode->addAttribute('price', $option['price']);
193: $_valueNode->addAttribute('formated_price', $option['formated_price']);
194: }
195:
196: 197: 198:
199: $_attrClone = $attributes;
200: if (next($_attrClone) != false) {
201: reset($_attrClone);
202: $this->_prepareRecursivelyRelatedValues($_valueNode, $_attrClone, $intersect, $cycle + 1);
203: }
204: }
205: }
206:
207: 208: 209: 210: 211: 212: 213: 214:
215: protected function _preparePrice($product, $price, $isPercent = false)
216: {
217: if ($isPercent && !empty($price)) {
218: $price = $product->getFinalPrice() * $price / 100;
219: }
220:
221: $price = Mage::app()->getStore()->convertPrice($price);
222: $price = Mage::app()->getStore()->roundPrice($price);
223: return $price;
224: }
225: }
226: