PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Block/Customer/Order/Item/Renderer/Bundle.php

https://gitlab.com/blingbang2016/shop
PHP | 334 lines | 217 code | 35 blank | 82 comment | 59 complexity | 72394c13ca338474becc38e99b520e17 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Customer order item xml renderer for bundle product type
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Customer_Order_Item_Renderer_Bundle extends Mage_Bundle_Block_Sales_Order_Items_Renderer
  34. {
  35. /**
  36. * Add item to XML object
  37. * (get from template: bundle/sales/order/items/renderer.phtml)
  38. *
  39. * @param Mage_XmlConnect_Model_Simplexml_Element $orderItemXmlObj
  40. * @return null
  41. */
  42. public function addItemToXmlObject(Mage_XmlConnect_Model_Simplexml_Element $orderItemXmlObj)
  43. {
  44. if ($this->getNewApi()) {
  45. $this->addItemToXmlObjectApi23($orderItemXmlObj);
  46. return;
  47. }
  48. /** @var $parentItem Mage_Sales_Model_Order_Item */
  49. $parentItem = $this->getItem();
  50. $items = array_merge(array($parentItem), $parentItem->getChildrenItems());
  51. $prevOptionId = '';
  52. /** @var $weeeHelper Mage_Weee_Helper_Data */
  53. $weeeHelper = $this->helper('weee');
  54. /** @var $taxHelper Mage_Tax_Helper_Data */
  55. $taxHelper = $this->helper('tax');
  56. /** @var $itemXml Mage_XmlConnect_Model_Simplexml_Element */
  57. $itemXml = $orderItemXmlObj->addChild('item');
  58. /** @var $optionsXml Mage_XmlConnect_Model_Simplexml_Element */
  59. $optionsXml = $itemXml->addChild('related_products');
  60. $this->setWeeeTaxAppliedAmount($parentItem->getWeeeTaxAppliedAmount());
  61. $this->setWeeeTaxDisposition($parentItem->getWeeeTaxDisposition());
  62. $typeOfDisplay1 = $weeeHelper->typeOfDisplay($parentItem, 1, 'sales')
  63. && $this->getWeeeTaxAppliedAmount();
  64. $typeOfDisplay2 = $weeeHelper->typeOfDisplay($parentItem, 2, 'sales')
  65. && $this->getWeeeTaxAppliedAmount();
  66. $typeOfDisplay4 = $weeeHelper->typeOfDisplay($parentItem, 4, 'sales')
  67. && $this->getWeeeTaxAppliedAmount();
  68. $typeOfDisplay014 = $weeeHelper->typeOfDisplay($parentItem, array(0, 1, 4), 'sales')
  69. && $this->getWeeeTaxAppliedAmount();
  70. $this->setTypesOfDisplay(array(
  71. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_1 => $typeOfDisplay1,
  72. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_2 => $typeOfDisplay2,
  73. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_4 => $typeOfDisplay4,
  74. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_14 => $typeOfDisplay014
  75. ));
  76. $this->setWeeeTaxes($weeeHelper->getApplied($parentItem));
  77. /** @var $item Mage_Sales_Model_Order_Item */
  78. foreach ($items as $item) {
  79. $isOption = $item->getParentItem() ? true : false;
  80. /** @var $objectXml Mage_XmlConnect_Model_Simplexml_Element */
  81. if ($isOption) {
  82. $objectXml = $optionsXml->addChild('item');
  83. } else {
  84. $objectXml = $itemXml;
  85. }
  86. $objectXml->addAttribute('product_id', $item->getProductId());
  87. $objectXml->addCustomChild('entity_type', $item->getProductType());
  88. if ($isOption) {
  89. $attributes = $this->getSelectionAttributes($item);
  90. if ($prevOptionId != $attributes['option_id']) {
  91. $objectXml->addAttribute('label', $objectXml->xmlAttribute($attributes['option_label']));
  92. $prevOptionId = $attributes['option_id'];
  93. }
  94. }
  95. $objectXml->addCustomChild('sku', Mage::helper('core/string')->splitInjection($item->getSku()));
  96. if ($isOption) {
  97. $name = $this->getValueHtml($item);
  98. } else {
  99. $name = $item->getName();
  100. }
  101. $objectXml->addCustomChild('name', $name);
  102. // set prices exactly for the Bundle product, but not for related products
  103. if (!$isOption) {
  104. /** @var $priceXml Mage_XmlConnect_Model_Simplexml_Element */
  105. $priceXml = $objectXml->addChild('price');
  106. /** @var $subtotalXml Mage_XmlConnect_Model_Simplexml_Element */
  107. $subtotalXml = $objectXml->addChild('subtotal');
  108. // Price excluding tax
  109. if ($taxHelper->displaySalesBothPrices() || $taxHelper->displaySalesPriceExclTax()) {
  110. Mage::helper('xmlconnect/customer_order')->addPriceAndSubtotalToXml(
  111. $this, $parentItem, $priceXml, $subtotalXml
  112. );
  113. }
  114. // Price including tax
  115. if ($taxHelper->displaySalesBothPrices() || $taxHelper->displaySalesPriceInclTax()) {
  116. Mage::helper('xmlconnect/customer_order')->addPriceAndSubtotalToXml(
  117. $this, $parentItem, $priceXml, $subtotalXml, true
  118. );
  119. }
  120. }
  121. // set quantities
  122. /** @var $qtyXml Mage_XmlConnect_Model_Simplexml_Element */
  123. if (($isOption && $this->isChildCalculated()) || (!$isOption && !$this->isChildCalculated())) {
  124. $qtyXml = $objectXml->addChild('qty');
  125. if ($item->getQtyOrdered() > 0) {
  126. $qtyXml->addCustomChild('value', $item->getQtyOrdered() * 1, array(
  127. 'label' => Mage::helper('sales')->__('Ordered')
  128. ));
  129. }
  130. if ($item->getQtyShipped() > 0 && !$this->isShipmentSeparately()) {
  131. $qtyXml->addCustomChild('value', $item->getQtyShipped() * 1, array(
  132. 'label' => Mage::helper('sales')->__('Shipped')
  133. ));
  134. }
  135. if ($item->getQtyCanceled() > 0) {
  136. $qtyXml->addCustomChild('value', $item->getQtyCanceled() * 1, array(
  137. 'label' => Mage::helper('sales')->__('Canceled')
  138. ));
  139. }
  140. if ($item->getQtyRefunded() > 0) {
  141. $qtyXml->addCustomChild('value', $item->getQtyRefunded() * 1, array(
  142. 'label' => Mage::helper('sales')->__('Refunded')
  143. ));
  144. }
  145. } elseif ($item->getQtyShipped() > 0 && $isOption && $this->isShipmentSeparately()) {
  146. $qtyXml = $objectXml->addChild('qty');
  147. $qtyXml->addCustomChild('value', $item->getQtyShipped() * 1, array(
  148. 'label' => Mage::helper('sales')->__('Shipped')
  149. ));
  150. }
  151. }
  152. if ($parentItem->getDescription()) {
  153. $itemXml->addCustomChild('description', $parentItem->getDescription());
  154. }
  155. Mage::helper('xmlconnect/customer_order')->addItemOptionsToXml($this, $itemXml);
  156. }
  157. /**
  158. * Add item to XML object. Api version 23
  159. *
  160. * @param Mage_XmlConnect_Model_Simplexml_Element $orderItemXmlObj
  161. * @return null
  162. */
  163. public function addItemToXmlObjectApi23(Mage_XmlConnect_Model_Simplexml_Element $orderItemXmlObj)
  164. {
  165. /** @var $parentItem Mage_Sales_Model_Order_Item */
  166. $parentItem = $this->getItem();
  167. $items = array_merge(array($parentItem), $parentItem->getChildrenItems());
  168. $prevOptionId = '';
  169. /** @var $weeeHelper Mage_Weee_Helper_Data */
  170. $weeeHelper = $this->helper('weee');
  171. /** @var $taxHelper Mage_Tax_Helper_Data */
  172. $taxHelper = $this->helper('tax');
  173. /** @var $itemXml Mage_XmlConnect_Model_Simplexml_Element */
  174. $itemXml = $orderItemXmlObj->addChild('item');
  175. /** @var $optionsXml Mage_XmlConnect_Model_Simplexml_Element */
  176. $optionsXml = $itemXml->addChild('related_products');
  177. $this->setWeeeTaxAppliedAmount($parentItem->getWeeeTaxAppliedAmount());
  178. $this->setWeeeTaxDisposition($parentItem->getWeeeTaxDisposition());
  179. $typeOfDisplay1 = $weeeHelper->typeOfDisplay($parentItem, 1, 'sales')
  180. && $this->getWeeeTaxAppliedAmount();
  181. $typeOfDisplay2 = $weeeHelper->typeOfDisplay($parentItem, 2, 'sales')
  182. && $this->getWeeeTaxAppliedAmount();
  183. $typeOfDisplay4 = $weeeHelper->typeOfDisplay($parentItem, 4, 'sales')
  184. && $this->getWeeeTaxAppliedAmount();
  185. $typeOfDisplay014 = $weeeHelper->typeOfDisplay($parentItem, array(0, 1, 4), 'sales')
  186. && $this->getWeeeTaxAppliedAmount();
  187. $this->setTypesOfDisplay(array(
  188. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_1 => $typeOfDisplay1,
  189. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_2 => $typeOfDisplay2,
  190. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_4 => $typeOfDisplay4,
  191. Mage_XmlConnect_Helper_Customer_Order::PRICE_DISPLAY_TYPE_14 => $typeOfDisplay014
  192. ));
  193. $this->setWeeeTaxes($weeeHelper->getApplied($parentItem));
  194. /** @var $item Mage_Sales_Model_Order_Item */
  195. foreach ($items as $item) {
  196. $isOption = $item->getParentItem() ? true : false;
  197. /** @var $objectXml Mage_XmlConnect_Model_Simplexml_Element */
  198. if ($isOption) {
  199. $objectXml = $optionsXml->addChild('item');
  200. } else {
  201. $objectXml = $itemXml;
  202. }
  203. $objectXml->addAttribute('product_id', $item->getProductId());
  204. $objectXml->addCustomChild('entity_type', $item->getProductType());
  205. if ($isOption) {
  206. $attributes = $this->getSelectionAttributes($item);
  207. if ($prevOptionId != $attributes['option_id']) {
  208. $objectXml->addAttribute('label', $objectXml->xmlAttribute($attributes['option_label']));
  209. $prevOptionId = $attributes['option_id'];
  210. }
  211. }
  212. $objectXml->addCustomChild('sku', Mage::helper('core/string')->splitInjection($item->getSku()));
  213. if ($isOption) {
  214. $name = $this->getValueHtml($item);
  215. } else {
  216. $name = $item->getName();
  217. }
  218. $objectXml->addCustomChild('name', $name);
  219. // set prices exactly for the Bundle product, but not for related products
  220. if (!$isOption) {
  221. $priceXml = $itemXml->addCustomChild('price_list');
  222. $priceInfoXml = $priceXml->addCustomChild('prices', null, array('id' => 'price'));
  223. $subtotalInfoXml = $priceXml->addCustomChild('prices', null, array('id' => 'subtotal'));
  224. // Price excluding tax
  225. if ($taxHelper->displaySalesBothPrices() || $taxHelper->displaySalesPriceExclTax()) {
  226. Mage::helper('xmlconnect/customer_order')->addPriceAndSubtotalToXmlApi23(
  227. $this, $parentItem, $priceInfoXml, $subtotalInfoXml
  228. );
  229. }
  230. // Price including tax
  231. if ($taxHelper->displaySalesBothPrices() || $taxHelper->displaySalesPriceInclTax()) {
  232. Mage::helper('xmlconnect/customer_order')->addPriceAndSubtotalToXmlApi23(
  233. $this, $parentItem, $priceInfoXml, $subtotalInfoXml, true
  234. );
  235. }
  236. }
  237. // set quantities
  238. /** @var $qtyXml Mage_XmlConnect_Model_Simplexml_Element */
  239. if (($isOption && $this->isChildCalculated()) || (!$isOption && !$this->isChildCalculated())) {
  240. $qtyXml = $objectXml->addChild('quantity');
  241. if ($item->getQtyOrdered() > 0) {
  242. $qtyXml->addCustomChild('value', $item->getQtyOrdered() * 1, array(
  243. 'label' => Mage::helper('sales')->__('Ordered')
  244. ));
  245. }
  246. if ($item->getQtyShipped() > 0 && !$this->isShipmentSeparately()) {
  247. $qtyXml->addCustomChild('value', $item->getQtyShipped() * 1, array(
  248. 'label' => Mage::helper('sales')->__('Shipped')
  249. ));
  250. }
  251. if ($item->getQtyCanceled() > 0) {
  252. $qtyXml->addCustomChild('value', $item->getQtyCanceled() * 1, array(
  253. 'label' => Mage::helper('sales')->__('Canceled')
  254. ));
  255. }
  256. if ($item->getQtyRefunded() > 0) {
  257. $qtyXml->addCustomChild('value', $item->getQtyRefunded() * 1, array(
  258. 'label' => Mage::helper('sales')->__('Refunded')
  259. ));
  260. }
  261. } elseif ($item->getQtyShipped() > 0 && $isOption && $this->isShipmentSeparately()) {
  262. $qtyXml = $objectXml->addChild('quantity');
  263. $qtyXml->addCustomChild('value', $item->getQtyShipped() * 1, array(
  264. 'label' => Mage::helper('sales')->__('Shipped')
  265. ));
  266. }
  267. }
  268. if ($parentItem->getDescription()) {
  269. $itemXml->addCustomChild('description', $parentItem->getDescription());
  270. }
  271. Mage::helper('xmlconnect/customer_order')->addItemOptionsToXml($this, $itemXml);
  272. }
  273. /**
  274. * Prepare option data for output
  275. *
  276. * @param Mage_Sales_Model_Order_Item $item
  277. * @return string
  278. */
  279. public function getValueHtml($item)
  280. {
  281. $attributes = $this->getSelectionAttributes($item);
  282. if ($attributes) {
  283. return sprintf('%d', $attributes['qty']) . ' x ' . $item->getName() . ' - '
  284. . $this->_formatPrice($attributes['price']);
  285. } else {
  286. return $item->getName();
  287. }
  288. }
  289. /**
  290. * Format price using order currency
  291. *
  292. * @param float $price
  293. * @return string
  294. */
  295. protected function _formatPrice($price)
  296. {
  297. return Mage::helper('xmlconnect/customer_order')->formatPrice($this, $price);
  298. }
  299. }