Magento – How to determine if an item has discount from catalog price rule

cartcatalog-price-rulescheckoutdiscountshopping-cart-price-rules

Any ideas how to make sure the item in the cart / quote was affected by CATALOG price rule (not shopping cart rule)? There is a similar question without an answer:
Do not apply catalog price rules on a product already on sale

Best Answer

Product to catalog price rules data is stored in the table catalogrule_product In that table you will find the product_id field that relates back to your product.

You should be able to build a query that checks the table for the product in question.

Not 100% what your intended functionality would involve, but doing a separate query for each product to check if they have an ACTIVE rule applying to them would slow your cart display down a fair bit. It would thus be better to make one query with all the products IDS you are interested in, and then use the resulting collection data to work your magic.

In case it helps you get a query built, I include below some code that I use in my Dynamic Category Products Extension which finds all products with an active rule. You should be able to adjust this and limit it to your products ids alone. It also takes the date range of the rule into consideration. Remember to consider that rules can be of two possible types: BY_PERCENT or FIXED Some bits are out of context, but you should get the jest of it.

$category below is the current active category. $value is simply the value of the discount I am interested in. $operator is self explanatory ;)

$collection = $category->getProductCollection();
$value = $this->getValueParsed();
$operator = $this->_operatorMapToSql[$this->getOperator()]; 
$conditions = " (price_rule.product_id = e.entity_id) ";
if (strpos($value, '%') > 0) {
            $value = str_replace('%', '', $value);
            $conditions .= " AND action_operator = '" . Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION . "' ";
        } else {
            $conditions .= " AND action_operator = '" . Mage_SalesRule_Model_Rule::BY_FIXED_ACTION . "' ";
        }
        $storeDate = Mage::app()->getLocale()->storeTimeStamp($this->getStoreId());
        $conditions .= " AND (from_time = 0
                    OR from_time <= " . $storeDate . ")
                    AND (to_time = 0
                    OR to_time >= " . $storeDate . ") ";
        $conditions .= " AND action_amount " . $operator . " " . $value;
        $collection->getSelect()->joinInner(
            array('price_rule' => $collection->getTable('catalogrule/rule_product')), $conditions
        );

I hope this get you closer to your solution

Related Topic