-
Notifications
You must be signed in to change notification settings - Fork 16
/
Observer.php
307 lines (271 loc) · 10.8 KB
/
Observer.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Bundle
* @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Bundle Products Observer
*
* @category Mage
* @package Mage_Bundle
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Bundle_Model_Observer
{
/**
* Setting Bundle Items Data to product for father processing
*
* @param Varien_Object $observer
* @return Mage_Bundle_Model_Observer
*/
public function prepareProductSave($observer)
{
$request = $observer->getEvent()->getRequest();
$product = $observer->getEvent()->getProduct();
if (($items = $request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
$product->setBundleOptionsData($items);
}
if (($selections = $request->getPost('bundle_selections')) && !$product->getCompositeReadonly()) {
$product->setBundleSelectionsData($selections);
}
if ($product->getPriceType() == '0' && !$product->getOptionsReadonly()) {
$product->setCanSaveCustomOptions(true);
if ($customOptions = $product->getProductOptions()) {
foreach (array_keys($customOptions) as $key) {
$customOptions[$key]['is_delete'] = 1;
}
$product->setProductOptions($customOptions);
}
}
$product->setCanSaveBundleSelections(
(bool)$request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly()
);
return $this;
}
/**
* Append bundles in upsell list for current product
*
* @param Varien_Object $observer
* @return Mage_Bundle_Model_Observer
*/
public function appendUpsellProducts($observer)
{
/* @var $product Mage_Catalog_Model_Product */
$product = $observer->getEvent()->getProduct();
/**
* Check is current product type is allowed for bundle selection product type
*/
if (!in_array($product->getTypeId(), Mage::helper('bundle')->getAllowedSelectionTypes())) {
return $this;
}
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection */
$collection = $observer->getEvent()->getCollection();
$limit = $observer->getEvent()->getLimit();
if (is_array($limit)) {
if (isset($limit['upsell'])) {
$limit = $limit['upsell'];
} else {
$limit = 0;
}
}
/* @var $resource Mage_Bundle_Model_Mysql4_Selection */
$resource = Mage::getResourceSingleton('bundle/selection');
$productIds = array_keys($collection->getItems());
if (!is_null($limit) && $limit <= count($productIds)) {
return $this;
}
// retrieve bundle product ids
$bundleIds = $resource->getParentIdsByChild($product->getId());
// exclude up-sell product ids
$bundleIds = array_diff($bundleIds, $productIds);
if (!$bundleIds) {
return $this;
}
/* @var $bundleCollection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
$bundleCollection = $product->getCollection()
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addStoreFilter()
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents();
Mage::getSingleton('catalog/product_visibility')
->addVisibleInCatalogFilterToCollection($bundleCollection);
if (!is_null($limit)) {
$bundleCollection->setPageSize($limit);
}
$bundleCollection->addFieldToFilter('entity_id', array('in' => $bundleIds))
->setFlag('do_not_use_category_id', true);
if ($collection instanceof Varien_Data_Collection) {
foreach ($bundleCollection as $item) {
$collection->addItem($item);
}
} elseif ($collection instanceof Varien_Object) {
$items = $collection->getItems();
foreach ($bundleCollection as $item) {
$items[$item->getEntityId()] = $item;
}
$collection->setItems($items);
}
return $this;
}
/**
* Append selection attributes to selection's order item
*
* @param Varien_Object $observer
* @return Mage_Bundle_Model_Observer
*/
public function appendBundleSelectionData($observer)
{
$orderItem = $observer->getEvent()->getOrderItem();
$quoteItem = $observer->getEvent()->getItem();
if ($attributes = $quoteItem->getProduct()->getCustomOption('bundle_selection_attributes')) {
$productOptions = $orderItem->getProductOptions();
$productOptions['bundle_selection_attributes'] = $attributes->getValue();
$orderItem->setProductOptions($productOptions);
}
return $this;
}
/**
* Add price index data for catalog product collection
* only for front end
*
* @param Varien_Event_Observer $observer
* @return Mage_Bundle_Model_Observer
*/
public function loadProductOptions($observer)
{
$collection = $observer->getEvent()->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
$collection->addPriceData();
return $this;
}
/**
* duplicating bundle options and selections
*
* @param Varien_Object $observer
* @return Mage_Bundle_Model_Observer
*/
public function duplicateProduct($observer)
{
$product = $observer->getEvent()->getCurrentProduct();
if ($product->getTypeId() != Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
//do nothing if not bundle
return $this;
}
$newProduct = $observer->getEvent()->getNewProduct();
$product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product);
$optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product);
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection(
$product->getTypeInstance(true)->getOptionsIds($product),
$product
);
$optionCollection->appendSelections($selectionCollection);
$optionRawData = array();
$selectionRawData = array();
$i = 0;
foreach ($optionCollection as $option) {
$optionRawData[$i] = array(
'required' => $option->getData('required'),
'position' => $option->getData('position'),
'type' => $option->getData('type'),
'title' => $option->getData('title')?$option->getData('title'):$option->getData('default_title'),
'delete' => ''
);
foreach ($option->getSelections() as $selection) {
$selectionRawData[$i][] = array(
'product_id' => $selection->getProductId(),
'position' => $selection->getPosition(),
'is_default' => $selection->getIsDefault(),
'selection_price_type' => $selection->getSelectionPriceType(),
'selection_price_value' => $selection->getSelectionPriceValue(),
'selection_qty' => $selection->getSelectionQty(),
'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
'delete' => ''
);
}
$i++;
}
$newProduct->setBundleOptionsData($optionRawData);
$newProduct->setBundleSelectionsData($selectionRawData);
return $this;
}
/**
* Setting attribute tab block for bundle
*
* @param Varien_Object $observer
* @return Mage_Bundle_Model_Observer
*/
public function setAttributeTabBlock($observer)
{
$product = $observer->getEvent()->getProduct();
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
Mage::helper('adminhtml/catalog')
->setAttributeTabBlock('bundle/adminhtml_catalog_product_edit_tab_attributes');
}
return $this;
}
/**
* Initialize product options renderer with bundle specific params
*
* @param Varien_Event_Observer $observer
* @return Mage_Bundle_Model_Observer
*/
public function initOptionRenderer(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
$block->addOptionsRenderCfg('bundle', 'bundle/catalog_product_configuration');
return $this;
}
/**
* Add price index to bundle product after load
*
* @deprecated since 1.4.0.0
*
* @param Varien_Event_Observer $observer
* @return Mage_Bundle_Model_Observer
*/
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
$product = $observer->getEvent()->getProduct();
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
Mage::getSingleton('bundle/price_index')
->addPriceIndexToProduct($product);
}
return $this;
}
/**
* CatalogIndex Indexer after plain reindex process
*
* @deprecated since 1.4.0.0
* @see Mage_Bundle_Model_Mysql4_Indexer_Price
*
* @param Varien_Event_Observer $observer
* @return Mage_Bundle_Model_Observer
*/
public function catalogIndexPlainReindexAfter(Varien_Event_Observer $observer)
{
$products = $observer->getEvent()->getProducts();
Mage::getSingleton('bundle/price_index')->reindex($products);
return $this;
}
}