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 extends Mage_XmlConnect_Block_Catalog
35: {
36: const OPTION_TYPE_SELECT = 'select';
37: const OPTION_TYPE_CHECKBOX = 'checkbox';
38: const OPTION_TYPE_TEXT = 'text';
39:
40: 41: 42: 43: 44:
45: protected $_renderers = array();
46:
47: 48: 49: 50: 51: 52: 53:
54: public function addRenderer($type, $renderer)
55: {
56: if (!isset($this->_renderers[$type])) {
57: $this->_renderers[$type] = $renderer;
58: }
59: return $this;
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function getProductCustomOptionsXmlObject(Mage_Catalog_Model_Product $product)
69: {
70: $xmlModel = Mage::getModel('xmlconnect/simplexml_element', '<product></product>');
71: $optionsNode = $xmlModel->addChild('options');
72:
73: if (!$product->getId()) {
74: return $xmlModel;
75: }
76: $xmlModel->addAttribute('id', $product->getId());
77: if (!$product->isSaleable() || !sizeof($product->getOptions())) {
78: return $xmlModel;
79: }
80:
81: foreach ($product->getOptions() as $option) {
82: $optionNode = $optionsNode->addChild('option');
83: $type = $this->_getOptionTypeForXmlByRealType($option->getType());
84: $code = 'options[' . $option->getId() . ']';
85: if ($type == self::OPTION_TYPE_CHECKBOX) {
86: $code .= '[]';
87: }
88: $optionNode->addAttribute('code', $code);
89: $optionNode->addAttribute('type', $type);
90: $optionNode->addAttribute('label', $xmlModel->escapeXml($option->getTitle()));
91: if ($option->getIsRequire()) {
92: $optionNode->addAttribute('is_required', 1);
93: }
94:
95: 96: 97:
98: $price = $option->getPrice();
99: if ($price) {
100: $optionNode->addAttribute('price', Mage::helper('xmlconnect')->formatPriceForXml($price));
101: $formattedPrice = Mage::app()->getStore($product->getStoreId())->formatPrice($price, false);
102: $optionNode->addAttribute('formated_price', $formattedPrice);
103: }
104: if ($type == self::OPTION_TYPE_CHECKBOX || $type == self::OPTION_TYPE_SELECT) {
105: foreach ($option->getValues() as $value) {
106: $valueNode = $optionNode->addChild('value');
107: $valueNode->addAttribute('code', $value->getId());
108: $valueNode->addAttribute('label', $xmlModel->escapeXml($value->getTitle()));
109:
110: if ($value->getPrice() != 0) {
111: $price = Mage::helper('xmlconnect')->formatPriceForXml($value->getPrice());
112: $valueNode->addAttribute('price', $price);
113: $formattedPrice = $this->_formatPriceString($price, $product);
114: $valueNode->addAttribute('formated_price', $formattedPrice);
115: }
116: }
117: }
118: }
119: return $xmlModel;
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: protected function _formatPriceString($price, $product)
130: {
131: $priceTax = Mage::helper('tax')->getPrice($product, $price);
132: $priceIncTax = Mage::helper('tax')->getPrice($product, $price, true);
133:
134: if (Mage::helper('tax')->displayBothPrices() && $priceTax != $priceIncTax) {
135: $formatted = Mage::helper('core')->currency($priceTax, true, false) . ' (+'
136: . Mage::helper('core')->currency($priceIncTax, true, false) . ' '
137: . Mage::helper('tax')->__('Incl. Tax') . ')';
138: } else {
139: $formatted = $this->helper('core')->currency($priceTax, true, false);
140: }
141:
142: return $formatted;
143: }
144:
145: 146: 147: 148: 149: 150:
151: protected function _getOptionTypeForXmlByRealType($realType)
152: {
153: switch ($realType) {
154: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_DROP_DOWN:
155: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_RADIO:
156: $type = self::OPTION_TYPE_SELECT;
157: break;
158: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_MULTIPLE:
159: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_CHECKBOX:
160: $type = self::OPTION_TYPE_CHECKBOX;
161: break;
162: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_FIELD:
163: case Mage_Catalog_Model_Product_Option::OPTION_TYPE_AREA:
164: default:
165: $type = self::OPTION_TYPE_TEXT;
166: break;
167: }
168: return $type;
169: }
170:
171: 172: 173: 174: 175: 176:
177: public function getProductOptionsXmlObject(Mage_Catalog_Model_Product $product)
178: {
179: if ($product->getId()) {
180: $type = $product->getTypeId();
181: if (isset($this->_renderers[$type])) {
182: $renderer = $this->getLayout()->createBlock($this->_renderers[$type]);
183: if ($renderer) {
184: return $renderer->getProductOptionsXml($product, true);
185: }
186: }
187: }
188: return false;
189: }
190:
191: 192: 193: 194: 195:
196: protected function _toHtml()
197: {
198: $productId = $this->getRequest()->getParam('id', null);
199: $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId());
200:
201: if ($productId) {
202: $product->load($productId);
203: }
204:
205: if ($product->getId()) {
206: $type = $product->getTypeId();
207: if (isset($this->_renderers[$type])) {
208: $renderer = $this->getLayout()->createBlock($this->_renderers[$type]);
209: if ($renderer) {
210: return $renderer->getProductOptionsXml($product);
211: }
212: }
213: }
214: return '<?xml version="1.0" encoding="UTF-8"?><options/>';
215: }
216: }
217: