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_Bundle_Model_Observer
35: {
36: 37: 38: 39: 40: 41:
42: public function prepareProductSave($observer)
43: {
44: $request = $observer->getEvent()->getRequest();
45: $product = $observer->getEvent()->getProduct();
46:
47: if (($items = $request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
48: $product->setBundleOptionsData($items);
49: }
50:
51: if (($selections = $request->getPost('bundle_selections')) && !$product->getCompositeReadonly()) {
52: $product->setBundleSelectionsData($selections);
53: }
54:
55: if ($product->getPriceType() == '0' && !$product->getOptionsReadonly()) {
56: $product->setCanSaveCustomOptions(true);
57: if ($customOptions = $product->getProductOptions()) {
58: foreach (array_keys($customOptions) as $key) {
59: $customOptions[$key]['is_delete'] = 1;
60: }
61: $product->setProductOptions($customOptions);
62: }
63: }
64:
65: $product->setCanSaveBundleSelections(
66: (bool)$request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly()
67: );
68:
69: return $this;
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function appendUpsellProducts($observer)
79: {
80:
81: $product = $observer->getEvent()->getProduct();
82:
83: 84: 85:
86: if (!in_array($product->getTypeId(), Mage::helper('bundle')->getAllowedSelectionTypes())) {
87: return $this;
88: }
89:
90:
91: $collection = $observer->getEvent()->getCollection();
92: $limit = $observer->getEvent()->getLimit();
93: if (is_array($limit)) {
94: if (isset($limit['upsell'])) {
95: $limit = $limit['upsell'];
96: } else {
97: $limit = 0;
98: }
99: }
100:
101:
102: $resource = Mage::getResourceSingleton('bundle/selection');
103:
104: $productIds = array_keys($collection->getItems());
105: if (!is_null($limit) && $limit <= count($productIds)) {
106: return $this;
107: }
108:
109:
110: $bundleIds = $resource->getParentIdsByChild($product->getId());
111:
112: $bundleIds = array_diff($bundleIds, $productIds);
113:
114: if (!$bundleIds) {
115: return $this;
116: }
117:
118:
119: $bundleCollection = $product->getCollection()
120: ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
121: ->addStoreFilter()
122: ->addMinimalPrice()
123: ->addFinalPrice()
124: ->addTaxPercents();
125:
126: Mage::getSingleton('catalog/product_visibility')
127: ->addVisibleInCatalogFilterToCollection($bundleCollection);
128:
129: if (!is_null($limit)) {
130: $bundleCollection->setPageSize($limit);
131: }
132: $bundleCollection->addFieldToFilter('entity_id', array('in' => $bundleIds))
133: ->setFlag('do_not_use_category_id', true);
134:
135: if ($collection instanceof Varien_Data_Collection) {
136: foreach ($bundleCollection as $item) {
137: $collection->addItem($item);
138: }
139: } elseif ($collection instanceof Varien_Object) {
140: $items = $collection->getItems();
141: foreach ($bundleCollection as $item) {
142: $items[$item->getEntityId()] = $item;
143: }
144: $collection->setItems($items);
145: }
146:
147: return $this;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function appendBundleSelectionData($observer)
157: {
158: $orderItem = $observer->getEvent()->getOrderItem();
159: $quoteItem = $observer->getEvent()->getItem();
160:
161: if ($attributes = $quoteItem->getProduct()->getCustomOption('bundle_selection_attributes')) {
162: $productOptions = $orderItem->getProductOptions();
163: $productOptions['bundle_selection_attributes'] = $attributes->getValue();
164: $orderItem->setProductOptions($productOptions);
165: }
166:
167: return $this;
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: public function loadProductOptions($observer)
178: {
179: $collection = $observer->getEvent()->getCollection();
180:
181: $collection->addPriceData();
182:
183: return $this;
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function duplicateProduct($observer)
193: {
194: $product = $observer->getEvent()->getCurrentProduct();
195:
196: if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
197:
198: return $this;
199: }
200:
201: $newProduct = $observer->getEvent()->getNewProduct();
202:
203: $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
204: $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
205: $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
206: $product->getTypeInstance(true)->getOptionsIds($product),
207: $product
208: );
209: $optionCollection->appendSelections($selectionCollection);
210:
211: $optionRawData = array();
212: $selectionRawData = array();
213:
214: $i = 0;
215: foreach ($optionCollection as $option) {
216: $optionRawData[$i] = array(
217: 'required' => $option->getData('required'),
218: 'position' => $option->getData('position'),
219: 'type' => $option->getData('type'),
220: 'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
221: 'delete' => ''
222: );
223: foreach ($option->getSelections() as $selection) {
224: $selectionRawData[$i][] = array(
225: 'product_id' => $selection->getProductId(),
226: 'position' => $selection->getPosition(),
227: 'is_default' => $selection->getIsDefault(),
228: 'selection_price_type' => $selection->getSelectionPriceType(),
229: 'selection_price_value' => $selection->getSelectionPriceValue(),
230: 'selection_qty' => $selection->getSelectionQty(),
231: 'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
232: 'delete' => ''
233: );
234: }
235: $i++;
236: }
237:
238: $newProduct->setBundleOptionsData($optionRawData);
239: $newProduct->setBundleSelectionsData($selectionRawData);
240: return $this;
241: }
242:
243: 244: 245: 246: 247: 248:
249: public function setAttributeTabBlock($observer)
250: {
251: $product = $observer->getEvent()->getProduct();
252: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
253: Mage::helper('adminhtml/catalog')
254: ->setAttributeTabBlock('bundle/adminhtml_catalog_product_edit_tab_attributes');
255: }
256: return $this;
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function initOptionRenderer(Varien_Event_Observer $observer)
266: {
267: $block = $observer->getBlock();
268: $block->addOptionsRenderCfg('bundle', 'bundle/catalog_product_configuration');
269: return $this;
270: }
271:
272: 273: 274: 275: 276: 277: 278: 279:
280: public function catalogProductLoadAfter(Varien_Event_Observer $observer)
281: {
282: $product = $observer->getEvent()->getProduct();
283: if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
284: Mage::getSingleton('bundle/price_index')
285: ->addPriceIndexToProduct($product);
286: }
287:
288: return $this;
289: }
290:
291: 292: 293: 294: 295: 296: 297: 298: 299:
300: public function catalogIndexPlainReindexAfter(Varien_Event_Observer $observer)
301: {
302: $products = $observer->getEvent()->getProducts();
303: Mage::getSingleton('bundle/price_index')->reindex($products);
304:
305: return $this;
306: }
307: }
308: