Skip to content

Instantly share code, notes, and snippets.

@IvanChepurnyi
Created September 16, 2013 22:31
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save IvanChepurnyi/6587481 to your computer and use it in GitHub Desktop.
Save IvanChepurnyi/6587481 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