Skip to content

Instantly share code, notes, and snippets.

@mwin007
Created June 30, 2016 15:13
Show Gist options
  • Save mwin007/35054cebe0fd75e684b884b300633081 to your computer and use it in GitHub Desktop.
Save mwin007/35054cebe0fd75e684b884b300633081 to your computer and use it in GitHub Desktop.
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();
}
}
}
<?php
/**
* 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment