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: class Mage_CatalogRule_Model_Observer
31: {
32: 33: 34: 35: 36: 37:
38: protected $_rulePrices = array();
39:
40: 41: 42: 43: 44: 45:
46: public function applyAllRulesOnProduct($observer)
47: {
48: $product = $observer->getEvent()->getProduct();
49: if ($product->getIsMassupdate()) {
50: return;
51: }
52:
53: $productWebsiteIds = $product->getWebsiteIds();
54:
55: $rules = Mage::getModel('catalogrule/rule')->getCollection()
56: ->addFieldToFilter('is_active', 1);
57:
58: foreach ($rules as $rule) {
59: $websiteIds = array_intersect($productWebsiteIds, $rule->getWebsiteIds());
60: $rule->applyToProduct($product, $websiteIds);
61: }
62:
63: return $this;
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73:
74: public function applyAllRules($observer)
75: {
76: $resource = Mage::getResourceSingleton('catalogrule/rule');
77: $resource->applyAllRulesForDateRange($resource->formatDate(mktime(0,0,0)));
78: Mage::getModel('catalogrule/flag')->loadSelf()
79: ->setState(0)
80: ->save();
81:
82: return $this;
83: }
84:
85: 86: 87: 88: 89: 90: 91:
92: public function processFrontFinalPrice($observer)
93: {
94: $product = $observer->getEvent()->getProduct();
95: $pId = $product->getId();
96: $storeId = $product->getStoreId();
97:
98: if ($observer->hasDate()) {
99: $date = $observer->getEvent()->getDate();
100: } else {
101: $date = Mage::app()->getLocale()->storeTimeStamp($storeId);
102: }
103:
104: if ($observer->hasWebsiteId()) {
105: $wId = $observer->getEvent()->getWebsiteId();
106: } else {
107: $wId = Mage::app()->getStore($storeId)->getWebsiteId();
108: }
109:
110: if ($observer->hasCustomerGroupId()) {
111: $gId = $observer->getEvent()->getCustomerGroupId();
112: } elseif ($product->hasCustomerGroupId()) {
113: $gId = $product->getCustomerGroupId();
114: } else {
115: $gId = Mage::getSingleton('customer/session')->getCustomerGroupId();
116: }
117:
118: $key = "$date|$wId|$gId|$pId";
119: if (!isset($this->_rulePrices[$key])) {
120: $rulePrice = Mage::getResourceModel('catalogrule/rule')
121: ->getRulePrice($date, $wId, $gId, $pId);
122: $this->_rulePrices[$key] = $rulePrice;
123: }
124: if ($this->_rulePrices[$key]!==false) {
125: $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
126: $product->setFinalPrice($finalPrice);
127: }
128: return $this;
129: }
130:
131: 132: 133: 134: 135: 136: 137:
138: public function processAdminFinalPrice($observer)
139: {
140: $product = $observer->getEvent()->getProduct();
141: $storeId = $product->getStoreId();
142: $date = Mage::app()->getLocale()->storeDate($storeId);
143: $key = false;
144:
145: if ($ruleData = Mage::registry('rule_data')) {
146: $wId = $ruleData->getWebsiteId();
147: $gId = $ruleData->getCustomerGroupId();
148: $pId = $product->getId();
149:
150: $key = "$date|$wId|$gId|$pId";
151: }
152: elseif (!is_null($product->getWebsiteId()) && !is_null($product->getCustomerGroupId())) {
153: $wId = $product->getWebsiteId();
154: $gId = $product->getCustomerGroupId();
155: $pId = $product->getId();
156: $key = "$date|$wId|$gId|$pId";
157: }
158:
159: if ($key) {
160: if (!isset($this->_rulePrices[$key])) {
161: $rulePrice = Mage::getResourceModel('catalogrule/rule')
162: ->getRulePrice($date, $wId, $gId, $pId);
163: $this->_rulePrices[$key] = $rulePrice;
164: }
165: if ($this->_rulePrices[$key]!==false) {
166: $finalPrice = min($product->getData('final_price'), $this->_rulePrices[$key]);
167: $product->setFinalPrice($finalPrice);
168: }
169: }
170:
171: return $this;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public function catalogProductTypeConfigurablePrice(Varien_Event_Observer $observer)
182: {
183: $product = $observer->getEvent()->getProduct();
184: if ($product instanceof Mage_Catalog_Model_Product
185: && $product->getConfigurablePrice() !== null
186: ) {
187: $configurablePrice = $product->getConfigurablePrice();
188: $productPriceRule = Mage::getModel('catalogrule/rule')->calcProductPriceRule($product, $configurablePrice);
189: if ($productPriceRule !== null) {
190: $product->setConfigurablePrice($productPriceRule);
191: }
192: }
193:
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204: 205: 206:
207: public function dailyCatalogUpdate($observer)
208: {
209: Mage::getResourceSingleton('catalogrule/rule')->applyAllRulesForDateRange();
210:
211: return $this;
212: }
213:
214: 215: 216:
217: public function flushPriceCache()
218: {
219: $this->_rulePrices = array();
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function prepareCatalogProductPriceIndexTable(Varien_Event_Observer $observer)
229: {
230: $select = $observer->getEvent()->getSelect();
231:
232: $indexTable = $observer->getEvent()->getIndexTable();
233: $entityId = $observer->getEvent()->getEntityId();
234: $customerGroupId = $observer->getEvent()->getCustomerGroupId();
235: $websiteId = $observer->getEvent()->getWebsiteId();
236: $websiteDate = $observer->getEvent()->getWebsiteDate();
237: $updateFields = $observer->getEvent()->getUpdateFields();
238:
239: Mage::getSingleton('catalogrule/rule_product_price')
240: ->applyPriceRuleToIndexTable($select, $indexTable, $entityId, $customerGroupId, $websiteId,
241: $updateFields, $websiteDate);
242:
243: return $this;
244: }
245:
246: 247: 248: 249: 250: 251: 252: 253:
254: protected function _checkCatalogRulesAvailability($attributeCode)
255: {
256:
257: $collection = Mage::getResourceModel('catalogrule/rule_collection')
258: ->addAttributeInConditionFilter($attributeCode);
259:
260: $disabledRulesCount = 0;
261: foreach ($collection as $rule) {
262:
263: $rule->setIsActive(0);
264:
265: $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
266: $rule->save();
267:
268: $disabledRulesCount++;
269: }
270:
271: if ($disabledRulesCount) {
272: Mage::getModel('catalogrule/rule')->applyAll();
273: Mage::getSingleton('adminhtml/session')->addWarning(
274: Mage::helper('catalogrule')->__('%d Catalog Price Rules based on "%s" attribute have been disabled.', $disabledRulesCount, $attributeCode));
275: }
276:
277: return $this;
278: }
279:
280: 281: 282: 283: 284: 285: 286:
287: protected function _removeAttributeFromConditions($combine, $attributeCode)
288: {
289: $conditions = $combine->getConditions();
290: foreach ($conditions as $conditionId => $condition) {
291: if ($condition instanceof Mage_CatalogRule_Model_Rule_Condition_Combine) {
292: $this->_removeAttributeFromConditions($condition, $attributeCode);
293: }
294: if ($condition instanceof Mage_Rule_Model_Condition_Product_Abstract) {
295: if ($condition->getAttribute() == $attributeCode) {
296: unset($conditions[$conditionId]);
297: }
298: }
299: }
300: $combine->setConditions($conditions);
301: }
302:
303: 304: 305: 306: 307: 308: 309:
310: public function catalogAttributeSaveAfter(Varien_Event_Observer $observer)
311: {
312: $attribute = $observer->getEvent()->getAttribute();
313: if ($attribute->dataHasChangedFor('is_used_for_promo_rules') && !$attribute->getIsUsedForPromoRules()) {
314: $this->_checkCatalogRulesAvailability($attribute->getAttributeCode());
315: }
316:
317: return $this;
318: }
319:
320: 321: 322: 323: 324: 325:
326: public function catalogAttributeDeleteAfter(Varien_Event_Observer $observer)
327: {
328: $attribute = $observer->getEvent()->getAttribute();
329: if ($attribute->getIsUsedForPromoRules()) {
330: $this->_checkCatalogRulesAvailability($attribute->getAttributeCode());
331: }
332:
333: return $this;
334: }
335:
336: public function prepareCatalogProductCollectionPrices(Varien_Event_Observer $observer)
337: {
338:
339: $collection = $observer->getEvent()->getCollection();
340: $store = Mage::app()->getStore($observer->getEvent()->getStoreId());
341: $websiteId = $store->getWebsiteId();
342: if ($observer->getEvent()->hasCustomerGroupId()) {
343: $groupId = $observer->getEvent()->getCustomerGroupId();
344: } else {
345:
346: $session = Mage::getSingleton('customer/session');
347: if ($session->isLoggedIn()) {
348: $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
349: } else {
350: $groupId = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
351: }
352: }
353: if ($observer->getEvent()->hasDate()) {
354: $date = $observer->getEvent()->getDate();
355: } else {
356: $date = Mage::app()->getLocale()->storeTimeStamp($store);
357: }
358:
359: $productIds = array();
360:
361: foreach ($collection as $product) {
362: $key = implode('|', array($date, $websiteId, $groupId, $product->getId()));
363: if (!isset($this->_rulePrices[$key])) {
364: $productIds[] = $product->getId();
365: }
366: }
367:
368: if ($productIds) {
369: $rulePrices = Mage::getResourceModel('catalogrule/rule')
370: ->getRulePrices($date, $websiteId, $groupId, $productIds);
371: foreach ($productIds as $productId) {
372: $key = implode('|', array($date, $websiteId, $groupId, $productId));
373: $this->_rulePrices[$key] = isset($rulePrices[$productId]) ? $rulePrices[$productId] : false;
374: }
375: }
376:
377: return $this;
378: }
379:
380: 381: 382: 383: 384:
385: public function createCatalogRulesRelations(Varien_Event_Observer $observer)
386: {
387: $adapter = $observer->getEvent()->getAdapter();
388: $affectedEntityIds = $adapter->getAffectedEntityIds();
389:
390: if (empty($affectedEntityIds)) {
391: return;
392: }
393:
394: $rules = Mage::getModel('catalogrule/rule')->getCollection()
395: ->addFieldToFilter('is_active', 1);
396:
397: foreach ($rules as $rule) {
398: $rule->setProductsFilter($affectedEntityIds);
399: Mage::getResourceSingleton('catalogrule/rule')->updateRuleProductData($rule);
400: }
401: }
402: }
403: