I am trying to add a special shopping cart price rule where a product is assigned to multiple categories and i want define a discount on a particular category the scenario is : There are two categories

  1. Category 1 , Category 2
  2. A Product is assigned to both categories
  3. If a product is comes in the cart from category 1 there should be discount on cart and if product is also on Category 2 no-discount should be applied.

What i have done for now is attached in the image below.

enter image description here

Any idea how this can be achieve ?


2 Answers 2


1.Create custom local module
2.config.xml required options:


3. Add row_config field to sales_flat_quote_item table via your installer:

$installer->getConnection()->addColumn($this->getTable('sales_flat_quote_item'), 'row_config', 'varchar(255) DEFAULT NULL');

4.Rewrite /app/code/core/Mage/SalesRule/Model/Rule/Condition/Product.php as following into your module folder /app/code/local/My/Module/Model/Rule/Condition/Product.php:

class My_Module_Model_Rule_Condition_Product extends Mage_Rule_Model_Condition_Product_Abstract
protected function _addSpecialAttributes(array &$attributes)
        $attributes['quote_item_qty'] = Mage::helper('salesrule')->__('Quantity in cart');
        $attributes['quote_item_price'] = Mage::helper('salesrule')->__('Price in cart');
        $attributes['quote_item_row_total'] = Mage::helper('salesrule')->__('Row total in cart');
        $attributes['quote_item_row_config'] = Mage::helper('salesrule')->__('Row config in cart');

public function validate(Varien_Object $object)
        $product = false;
        if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
            $product = $object->getProduct();
        } else {
            $product = Mage::getModel('catalog/product')

            ->setQuoteItemPrice($object->getPrice()) // possible bug: need to use $object->getBasePrice()

        return parent::validate($product);

6. Create observer /app/code/local/My/Module/Model/Observer.php:

class My_Module_Model_Observer{
function checkoutCartProductAddAfter($observer)
    $quoteItem = $observer->getEvent()->getQuoteItem();
    $config    = $quoteItem->getRowConfig();
    if (Mage::getSingleton('catalog/session')->getLastVisitedCategoryId()) {
        $config = $config . '|C' . Mage::getSingleton('catalog/session')->getLastVisitedCategoryId() . '|';

Observer fires when you add some product to cart and it add last visited category id to the cart item as "|C12|", there 12 is some category id. Finally you can create shopping cart price rules for cart item with particular category: enter image description here Here is my result: enter image description here

P.S: My Magento version is and I don't know this snippet works for your version, but you can use this logic.


I don't think this is possible by default. The relation product<->category is a many to many relation that only makes sense in the catalog listing and managing section. When adding a product in the cart the relation is "valid" only in one direction: product->category (is checked if product X is/is not in category Y)
You are adding the product entity to the cart. The cart does not remember the category you come from.
In theory you could implement this, but I don't exactly the cost of this.
You will have to pass to the addToCart method an additional parameter telling the category it comes from that you should add to the quote_item entity (a new field in sales_flat_quote_item and sales_flat_quote_address_item tables). Also you will have to modify the behavior of the cart rules to take into account your new field.
This is how I would do it if I had to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.