3

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 ?

Thanks

6

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

   <global>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <your_module_unique_node>
                        <type>singleton</type>
                        <class>My_Module_Model_Observer</class>
                        <method>checkoutCartProductAddAfter</method>
                    </your_module_unique_node>
                </observers>
            </checkout_cart_product_add_after>
        </events>
        <models>
            <salesrule>
                <rewrite>
                    <rule_condition_product>My_Module_Model_Rule_Condition_Product</rule_condition_product>
                </rewrite>
            </salesrule>
        </models>
    </global>


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)
    {
        parent::_addSpecialAttributes($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')
                ->load($object->getProductId());
        }

        $product
            ->setQuoteItemQty($object->getQty())
            ->setQuoteItemPrice($object->getPrice()) // possible bug: need to use $object->getBasePrice()
            ->setQuoteItemRowTotal($object->getBaseRowTotal())
            ->setQuoteItemRowConfig($object->getRowConfig());

        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() . '|';
    }
    $quoteItem->setRowConfig($config);
}
}

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 1.7.0.2 and I don't know this snippet works for your version, but you can use this logic.

0

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, privacy policy and cookie policy

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