Skip to content

Instantly share code, notes, and snippets.

@mwin007
Forked from IvanChepurnyi/Observer.php
Created July 1, 2016 20:48
Show Gist options
  • Save mwin007/d3046844e0f7bb8cd29783361416c626 to your computer and use it in GitHub Desktop.
Save mwin007/d3046844e0f7bb8cd29783361416c626 to your computer and use it in GitHub Desktop.
Optimized catalog price rules
<?php
class EcomDev_Optimization_Model_Rule_Observer extends Mage_CatalogRule_Model_Observer
{
protected $_preloadedPrices = array();
public function beforeCollectTotals(Varien_Event_Observer $observer)
{
$quote = $observer->getQuote();
$date = Mage::app()->getLocale()->storeTimeStamp($quote->getStoreId());
$websiteId = $quote->getStore()->getWebsiteId();
$groupId = $quote->getCustomerGroupId();
$productIds = array();
foreach ($quote->getAllItems() as $item) {
$productIds[] = $item->getProductId();
}
$cacheKey = spl_object_hash($quote);
if (!isset($this->_preloadedPrices[$cacheKey])) {
$this->_preloadedPrices[$cacheKey] = Mage::getResourceSingleton('catalogrule/rule')
->getRulePrices($date, $websiteId, $groupId, $productIds);
}
foreach ($this->_preloadedPrices[$cacheKey] as $productId => $price) {
$key = implode('|', array($date, $websiteId, $groupId, $productId));
$this->_rulePrices[$key] = $price;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment