PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rgranadino/magento-mirror
PHP | 282 lines | 151 code | 35 blank | 96 comment | 22 complexity | d586fdd19116acc1a298b18554ceb519 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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Bundle
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.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((bool)$request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly());
  61. return $this;
  62. }
  63. /**
  64. * Append bundles in upsell list for current product
  65. *
  66. * @param Varien_Object $observer
  67. * @return Mage_Bundle_Model_Observer
  68. */
  69. public function appendUpsellProducts($observer)
  70. {
  71. /* @var $product Mage_Catalog_Model_Product */
  72. $product = $observer->getEvent()->getProduct();
  73. /**
  74. * Check is current product type is allowed for bundle selection product type
  75. */
  76. if (!in_array($product->getTypeId(), Mage::helper('bundle')->getAllowedSelectionTypes())) {
  77. return $this;
  78. }
  79. /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection */
  80. $collection = $observer->getEvent()->getCollection();
  81. $limit = $observer->getEvent()->getLimit();
  82. if (is_array($limit)) {
  83. if (isset($limit['upsell'])) {
  84. $limit = $limit['upsell'];
  85. } else {
  86. $limit = 0;
  87. }
  88. }
  89. /* @var $resource Mage_Bundle_Model_Mysql4_Selection */
  90. $resource = Mage::getResourceSingleton('bundle/selection');
  91. $productIds = array_keys($collection->getItems());
  92. if ($limit <= count($productIds)) {
  93. return $this;
  94. }
  95. // retrieve bundle product ids
  96. $bundleIds = $resource->getParentIdsByChild($product->getId());
  97. // exclude up-sell product ids
  98. $bundleIds = array_diff($bundleIds, $productIds);
  99. if (!$bundleIds) {
  100. return $this;
  101. }
  102. /* @var $bundleCollection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
  103. $bundleCollection = $product->getCollection()
  104. ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
  105. ->addStoreFilter()
  106. ->addMinimalPrice()
  107. ->addFinalPrice()
  108. ->addTaxPercents();
  109. Mage::getSingleton('catalog/product_visibility')
  110. ->addVisibleInCatalogFilterToCollection($bundleCollection);
  111. $bundleCollection->setPageSize($limit - count($productIds))
  112. ->addFieldToFilter('entity_id', array('in' => $bundleIds))
  113. ->setFlag('do_not_use_category_id', true);
  114. foreach ($bundleCollection as $item) {
  115. $collection->addItem($item);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Append selection attributes to selection's order item
  121. *
  122. * @param Varien_Object $observer
  123. * @return Mage_Bundle_Model_Observer
  124. */
  125. public function appendBundleSelectionData($observer)
  126. {
  127. $orderItem = $observer->getEvent()->getOrderItem();
  128. $quoteItem = $observer->getEvent()->getItem();
  129. if ($attributes = $quoteItem->getProduct()->getCustomOption('bundle_selection_attributes')) {
  130. $productOptions = $orderItem->getProductOptions();
  131. $productOptions['bundle_selection_attributes'] = $attributes->getValue();
  132. $orderItem->setProductOptions($productOptions);
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Add price index data for catalog product collection
  138. * only for front end
  139. *
  140. * @param Varien_Event_Observer $observer
  141. * @return Mage_Bundle_Model_Observer
  142. */
  143. public function loadProductOptions($observer)
  144. {
  145. $collection = $observer->getEvent()->getCollection();
  146. /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
  147. $collection->addPriceData();
  148. return $this;
  149. }
  150. /**
  151. * duplicating bundle options and selections
  152. *
  153. * @param Varien_Object $observer
  154. * @return Mage_Bundle_Model_Observer
  155. */
  156. public function duplicateProduct($observer)
  157. {
  158. $product = $observer->getEvent()->getCurrentProduct();
  159. if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  160. //do nothing if not bundle
  161. return $this;
  162. }
  163. $newProduct = $observer->getEvent()->getNewProduct();
  164. $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
  165. $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
  166. $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
  167. $product->getTypeInstance(true)->getOptionsIds($product),
  168. $product
  169. );
  170. $optionCollection->appendSelections($selectionCollection);
  171. $optionRawData = array();
  172. $selectionRawData = array();
  173. $i = 0;
  174. foreach ($optionCollection as $option) {
  175. $optionRawData[$i] = array(
  176. 'required' => $option->getData('required'),
  177. 'position' => $option->getData('position'),
  178. 'type' => $option->getData('type'),
  179. 'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
  180. 'delete' => ''
  181. );
  182. foreach ($option->getSelections() as $selection) {
  183. $selectionRawData[$i][] = array(
  184. 'product_id' => $selection->getProductId(),
  185. 'position' => $selection->getPosition(),
  186. 'is_default' => $selection->getIsDefault(),
  187. 'selection_price_type' => $selection->getSelectionPriceType(),
  188. 'selection_price_value' => $selection->getSelectionPriceValue(),
  189. 'selection_qty' => $selection->getSelectionQty(),
  190. 'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
  191. 'delete' => ''
  192. );
  193. }
  194. $i++;
  195. }
  196. $newProduct->setBundleOptionsData($optionRawData);
  197. $newProduct->setBundleSelectionsData($selectionRawData);
  198. return $this;
  199. }
  200. /**
  201. * Setting attribute tab block for bundle
  202. *
  203. * @param Varien_Object $observer
  204. * @return Mage_Bundle_Model_Observer
  205. */
  206. public function setAttributeTabBlock($observer)
  207. {
  208. $product = $observer->getEvent()->getProduct();
  209. if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  210. Mage::helper('adminhtml/catalog')
  211. ->setAttributeTabBlock('bundle/adminhtml_catalog_product_edit_tab_attributes');
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Add price index to bundle product after load
  217. *
  218. * @deprecated since 1.4.0.0
  219. *
  220. * @param Varien_Event_Observer $observer
  221. * @return Mage_Bundle_Model_Observer
  222. */
  223. public function catalogProductLoadAfter(Varien_Event_Observer $observer)
  224. {
  225. $product = $observer->getEvent()->getProduct();
  226. if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
  227. Mage::getSingleton('bundle/price_index')
  228. ->addPriceIndexToProduct($product);
  229. }
  230. return $this;
  231. }
  232. /**
  233. * CatalogIndex Indexer after plain reindex process
  234. *
  235. * @deprecated since 1.4.0.0
  236. * @see Mage_Bundle_Model_Mysql4_Indexer_Price
  237. *
  238. * @param Varien_Event_Observer $observer
  239. * @return Mage_Bundle_Model_Observer
  240. */
  241. public function catalogIndexPlainReindexAfter(Varien_Event_Observer $observer)
  242. {
  243. $products = $observer->getEvent()->getProducts();
  244. Mage::getSingleton('bundle/price_index')->reindex($products);
  245. return $this;
  246. }
  247. }