PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Bundle/Model/Observer.php

https://github.com/speedupmate/Magento-CE-Mirror
PHP | 307 lines | 169 code | 36 blank | 102 comment | 25 complexity | 5c87799d1ef53ccf2838846c19444d67 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_Bundle
  23. * @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Bundle Products Observer
  28. *
  29. * @category Mage
  30. * @package Mage_Bundle
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Bundle_Model_Observer
  34. {
  35. /**
  36. * Setting Bundle Items Data to product for father processing
  37. *
  38. * @param Varien_Object $observer
  39. * @return Mage_Bundle_Model_Observer
  40. */
  41. public function prepareProductSave($observer)
  42. {
  43. $request = $observer->getEvent()->getRequest();
  44. $product = $observer->getEvent()->getProduct();
  45. if (($items = $request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
  46. $product->setBundleOptionsData($items);
  47. }
  48. if (($selections = $request->getPost('bundle_selections')) && !$product->getCompositeReadonly()) {
  49. $product->setBundleSelectionsData($selections);
  50. }
  51. if ($product->getPriceType() == '0' && !$product->getOptionsReadonly()) {
  52. $product->setCanSaveCustomOptions(true);
  53. if ($customOptions = $product->getProductOptions()) {
  54. foreach (array_keys($customOptions) as $key) {
  55. $customOptions[$key]['is_delete'] = 1;
  56. }
  57. $product->setProductOptions($customOptions);
  58. }
  59. }
  60. $product->setCanSaveBundleSelections(
  61. (bool)$request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly()
  62. );
  63. return $this;
  64. }
  65. /**
  66. * Append bundles in upsell list for current product
  67. *
  68. * @param Varien_Object $observer
  69. * @return Mage_Bundle_Model_Observer
  70. */
  71. public function appendUpsellProducts($observer)
  72. {
  73. /* @var $product Mage_Catalog_Model_Product */
  74. $product = $observer->getEvent()->getProduct();
  75. /**
  76. * Check is current product type is allowed for bundle selection product type
  77. */
  78. if (!in_array($product->getTypeId(), Mage::helper('bundle')->getAllowedSelectionTypes())) {
  79. return $this;
  80. }
  81. /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection */
  82. $collection = $observer->getEvent()->getCollection();
  83. $limit = $observer->getEvent()->getLimit();
  84. if (is_array($limit)) {
  85. if (isset($limit['upsell'])) {
  86. $limit = $limit['upsell'];
  87. } else {
  88. $limit = 0;
  89. }
  90. }
  91. /* @var $resource Mage_Bundle_Model_Mysql4_Selection */
  92. $resource = Mage::getResourceSingleton('bundle/selection');
  93. $productIds = array_keys($collection->getItems());
  94. if (!is_null($limit) && $limit <= count($productIds)) {
  95. return $this;
  96. }
  97. // retrieve bundle product ids
  98. $bundleIds = $resource->getParentIdsByChild($product->getId());
  99. // exclude up-sell product ids
  100. $bundleIds = array_diff($bundleIds, $productIds);
  101. if (!$bundleIds) {
  102. return $this;
  103. }
  104. /* @var $bundleCollection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
  105. $bundleCollection = $product->getCollection()
  106. ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
  107. ->addStoreFilter()
  108. ->addMinimalPrice()
  109. ->addFinalPrice()
  110. ->addTaxPercents();
  111. Mage::getSingleton('catalog/product_visibility')
  112. ->addVisibleInCatalogFilterToCollection($bundleCollection);
  113. if (!is_null($limit)) {
  114. $bundleCollection->setPageSize($limit);
  115. }
  116. $bundleCollection->addFieldToFilter('entity_id', array('in' => $bundleIds))
  117. ->setFlag('do_not_use_category_id', true);
  118. if ($collection instanceof Varien_Data_Collection) {
  119. foreach ($bundleCollection as $item) {
  120. $collection->addItem($item);
  121. }
  122. } elseif ($collection instanceof Varien_Object) {
  123. $items = $collection->getItems();
  124. foreach ($bundleCollection as $item) {
  125. $items[$item->getEntityId()] = $item;
  126. }
  127. $collection->setItems($items);
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Append selection attributes to selection's order item
  133. *
  134. * @param Varien_Object $observer
  135. * @return Mage_Bundle_Model_Observer
  136. */
  137. public function appendBundleSelectionData($observer)
  138. {
  139. $orderItem = $observer->getEvent()->getOrderItem();
  140. $quoteItem = $observer->getEvent()->getItem();
  141. if ($attributes = $quoteItem->getProduct()->getCustomOption('bundle_selection_attributes')) {
  142. $productOptions = $orderItem->getProductOptions();
  143. $productOptions['bundle_selection_attributes'] = $attributes->getValue();
  144. $orderItem->setProductOptions($productOptions);
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Add price index data for catalog product collection
  150. * only for front end
  151. *
  152. * @param Varien_Event_Observer $observer
  153. * @return Mage_Bundle_Model_Observer
  154. */
  155. public function loadProductOptions($observer)
  156. {
  157. $collection = $observer->getEvent()->getCollection();
  158. /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
  159. $collection->addPriceData();
  160. return $this;
  161. }
  162. /**
  163. * duplicating bundle options and selections
  164. *
  165. * @param Varien_Object $observer
  166. * @return Mage_Bundle_Model_Observer
  167. */
  168. public function duplicateProduct($observer)
  169. {
  170. $product = $observer->getEvent()->getCurrentProduct();
  171. if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  172. //do nothing if not bundle
  173. return $this;
  174. }
  175. $newProduct = $observer->getEvent()->getNewProduct();
  176. $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
  177. $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
  178. $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
  179. $product->getTypeInstance(true)->getOptionsIds($product),
  180. $product
  181. );
  182. $optionCollection->appendSelections($selectionCollection);
  183. $optionRawData = array();
  184. $selectionRawData = array();
  185. $i = 0;
  186. foreach ($optionCollection as $option) {
  187. $optionRawData[$i] = array(
  188. 'required' => $option->getData('required'),
  189. 'position' => $option->getData('position'),
  190. 'type' => $option->getData('type'),
  191. 'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
  192. 'delete' => ''
  193. );
  194. foreach ($option->getSelections() as $selection) {
  195. $selectionRawData[$i][] = array(
  196. 'product_id' => $selection->getProductId(),
  197. 'position' => $selection->getPosition(),
  198. 'is_default' => $selection->getIsDefault(),
  199. 'selection_price_type' => $selection->getSelectionPriceType(),
  200. 'selection_price_value' => $selection->getSelectionPriceValue(),
  201. 'selection_qty' => $selection->getSelectionQty(),
  202. 'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
  203. 'delete' => ''
  204. );
  205. }
  206. $i++;
  207. }
  208. $newProduct->setBundleOptionsData($optionRawData);
  209. $newProduct->setBundleSelectionsData($selectionRawData);
  210. return $this;
  211. }
  212. /**
  213. * Setting attribute tab block for bundle
  214. *
  215. * @param Varien_Object $observer
  216. * @return Mage_Bundle_Model_Observer
  217. */
  218. public function setAttributeTabBlock($observer)
  219. {
  220. $product = $observer->getEvent()->getProduct();
  221. if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  222. Mage::helper('adminhtml/catalog')
  223. ->setAttributeTabBlock('bundle/adminhtml_catalog_product_edit_tab_attributes');
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Initialize product options renderer with bundle specific params
  229. *
  230. * @param Varien_Event_Observer $observer
  231. * @return Mage_Bundle_Model_Observer
  232. */
  233. public function initOptionRenderer(Varien_Event_Observer $observer)
  234. {
  235. $block = $observer->getBlock();
  236. $block->addOptionsRenderCfg('bundle', 'bundle/catalog_product_configuration');
  237. return $this;
  238. }
  239. /**
  240. * Add price index to bundle product after load
  241. *
  242. * @deprecated since 1.4.0.0
  243. *
  244. * @param Varien_Event_Observer $observer
  245. * @return Mage_Bundle_Model_Observer
  246. */
  247. public function catalogProductLoadAfter(Varien_Event_Observer $observer)
  248. {
  249. $product = $observer->getEvent()->getProduct();
  250. if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  251. Mage::getSingleton('bundle/price_index')
  252. ->addPriceIndexToProduct($product);
  253. }
  254. return $this;
  255. }
  256. /**
  257. * CatalogIndex Indexer after plain reindex process
  258. *
  259. * @deprecated since 1.4.0.0
  260. * @see Mage_Bundle_Model_Mysql4_Indexer_Price
  261. *
  262. * @param Varien_Event_Observer $observer
  263. * @return Mage_Bundle_Model_Observer
  264. */
  265. public function catalogIndexPlainReindexAfter(Varien_Event_Observer $observer)
  266. {
  267. $products = $observer->getEvent()->getProducts();
  268. Mage::getSingleton('bundle/price_index')->reindex($products);
  269. return $this;
  270. }
  271. }