I think many of you have heard about such a remarkable Mаgentо feature as Catalog Price Rule.
During one of our recent projects we faced a serious issue: our client had 1 website, 17 store views, over 50000 products and 200 Catalog Price rules. And he complained that the rules were no longer applying.
During the investigation we found out that each time you create a new rule or edit an existing one and complete the process by clicking “Save and apply” Mаgentо rewrites all existing rules. I.e he method applyRules() is called during the function of saveAction() of the controller Mage_Adminhtml_Promo_CatalogController.
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 |
/** * Apply all active catalog price rules */ public function applyRulesAction() { $errorMessage = Mage::helper('catalogrule')->__('Unable to apply rules.'); try { Mage::getModel('catalogrule/rule')->applyAll(); //the major culprit Mage::getModel('catalogrule/flag')->loadSelf() ->setState(0) ->save(); $this->_getSession()->addSuccess(Mage::helper('catalogrule')->__('The rules have been applied.')); } catch (Mage_Core_Exception $e) { $this->_getSession()->addError($errorMessage . ' ' . $e->getMessage()); } catch (Exception $e) { $this->_getSession()->addError($errorMessage); } $this->_redirect('*/*'); } /** * Apply all price rules, invalidate related cache and refresh price index * * @return Mage_CatalogRule_Model_Rule */ public function applyAll() { $this->getResourceCollection()->walk(array($this->_getResource(), 'updateRuleProductData')); //the function 'updateRuleProductData' is applied by turn to each rule $this->_getResource()->applyAllRulesForDateRange(); $this->_invalidateCache(); $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price'); if ($indexProcess) { $indexProcess->reindexAll(); } } |
So I created a module which, instead of rewriting all rules, recalculates only that one which has been edited. So now only a single rule is transmitted to the function updateRuleProductData(), which helped to solve the performance problem.
Partner With Us
Let's discuss how to grow your business. Get a Free Quote.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Belvg_SmartyApplyCatalogRule_Model_Rule extends Mage_CatalogRule_Model_Rule { public function applyRule($rule_id) { $rule = Mage::getModel('catalogrule/rule')->load($rule_id); $this->_getResource()->updateRuleProductData($rule); //solution of the problem $this->_getResource()->applyAllRulesForDateRange(); $this->_invalidateCache(); $indexProcess = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_product_price'); if ($indexProcess) { $indexProcess->reindexAll(); } } } |
Due to this module changes to the database are made only for a certain rules and there is no chain reaction (re-saving the rest of the rules).
Hello,
unfortunately, there is no spare time at all.
However, you could create it by yourself, using all the information above.
Kindly,
Alex
Would you mind creating a pull request here with your fix? (and sharingthis)
https://github.com/OpenMage/magento-lts
Manaf,
By default Magento is saving all rules while the module is changing only that one which has been changed.
– (re-saving the rest of the rules)
here what is the issue while re-saving the rest of the rules? and how this becomes a for the customer’s complaint : ” And he complained that the rules were no longer applying.”