Tuesday , December 12 2017
Home >> All tutorial >> Magento Shopping Cart Promotion Rule for Product with Custom Options

Magento Shopping Cart Promotion Rule for Product with Custom Options

Magento is a great eCommerce platform, it comes with so many built in features some of which you will probably never use. However one feature that we hear a lot of complain about is the lack of possibility to have a Shopping Cart Promotion Rule that targets the products custom options. If you login to Magento admin, and go to Promotions > Shopping Cart Price Rules > Add New Rule, then click on the Conditions tab of the Shopping Cart Price Rule edit screen you will see a screen like the one shown below.

Shopping Cart Price Rule

As you can see on that screen, there is no mention of products custom options, only its attributes.

In this article I’m gonna show you a nice little, cheap trick on how you can achieve the effect of having Shopping Cart Promotion Rule for Product with Custom Options. It will require a few lines of code to add and a little tiny bit of “manual calculation”. We will extract the code into our special little Inchoo_QuoteItemRule extension. Before we start writing the code, lets first explain what “Quote” has to do with “Custom Options”. Imagine we have a product product with SKU assigned “test_no2″. That product has two custom options (Color & size) and every option value has its SKU assigned as well like those shown on the image below.

Product Custom Options

Now if we add that product to cart a product with selected Green color and Medium size option then this will get recorded under sales_flat_quote_item database table as entry with SKU value of “test_no2-color_green-size_m“, which is actualy and entry with the following formula:

product main SKU + “-” + first custom option SKU + “-” + second custom option SKU + “-” + … nth custom option SKU.

So the idea is that we actually create a rule condition that would allow us to have a Cart Item Attribute condition, like shown on image below, since Cart Item Attribute is actually a sales_flat_quote_item table entry field to put it like that. And we said that adding a product with custom option to cart results in sales_flat_quote_item table entry with SKU value of “test_no2-color_green-size_m“.

Now lets jump into the code, we will need 3 files to achieve the desired functionality.

app/etc/modules/Inchoo_QuoteItemRule.xml:

  1. <?xml version=“1.0”?>
  2. <config>
  3.     <modules>
  4.         <Inchoo_QuoteItemRule>
  5.             <active>true</active>
  6.             <codePool>community</codePool>
  7.         </Inchoo_QuoteItemRule>
  8.     </modules>
  9. </config>
app/code/community/Inchoo/QuoteItemRule/etc/config.xml:
  1. <?xml version=“1.0”?>
  2. <config>
  3.     <modules>
  4.         <Inchoo_QuoteItemRule>
  5.             <version>1.0.0.0</version>
  6.         </Inchoo_QuoteItemRule>
  7.     </modules>
  8.     <global>
  9.         <models>
  10.             <salesrule>
  11.                 <rewrite>
  12.                     <rule_condition_product>Inchoo_QuoteItemRule_Model_SalesRule_Rule_Condition_Product</rule_condition_product>
  13.                 </rewrite>
  14.             </salesrule>
  15.         </models>
  16.     </global>
  17. </config>
app/code/community/Inchoo/QuoteItemRule/Model/SalesRule/Rule/Condition/Product.php:
  1. <?php
  2. class Inchoo_QuoteItemRule_Model_SalesRule_Rule_Condition_Product extends Mage_Rule_Model_Condition_Product_Abstract
  3. {
  4.     protected function _addSpecialAttributes(array >$attributes)
  5.     {
  6.         parent::_addSpecialAttributes($attributes);
  7.         $attributes[‘quote_item_qty’] = Mage::helper(‘salesrule’)->__(‘Quantity in cart’);
  8.         $attributes[‘quote_item_price’] = Mage::helper(‘salesrule’)->__(‘Price in cart’);
  9.         $attributes[‘quote_item_row_total’] = Mage::helper(‘salesrule’)->__(‘Row total in cart’);
  10.         /* @inchoo */
  11.         $attributes[‘quote_item_sku’] = Mage::helper(‘salesrule’)->__(‘SKU’);
  12.         /* @inchoo */
  13.     }
  14.     public function validate(Varien_Object $object)
  15.     {
  16.         $product = false;
  17.         if ($object->getProduct() instanceof Mage_Catalog_Model_Product) {
  18.             $product = $object->getProduct();
  19.         } else {
  20.             $product = Mage::getModel(‘catalog/product’)
  21.                 ->load($object->getProductId());
  22.         }
  23.         $product
  24.             ->setQuoteItemQty($object->getQty())
  25.             ->setQuoteItemPrice($object->getPrice())
  26.             ->setQuoteItemRowTotal($object->getBaseRowTotal())
  27.             /* @inchoo */
  28.             ->setQuoteItemSku($object->getSku())
  29.             /* @inchoo */;
  30.         return parent::validate($product);
  31.     }
  32. }
We basically just needed to add two lines of code for the whole thing to work. Now if you revisit the Promotions > Shopping Cart Price Rules > Add New Rule, then click on the Conditions tab, select the Product Attribute Combination as first condition then under the next condition dropdown you will see SKU under Cart Item Attribute like shown on the screenshot above. So finally, we can create a Shopping Cart Promotion Rule for Product with Custom Options like shown on the images below.
Hope it helps.
Source: inchoo.net

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

3 comments

  1. Hi there,

    I think there’s a problem in your code. By changing the core file i get the result, but by making an extension (as you did), it doesn’t work. Any idea?

    Thank you! This has been of great help to me. Just that I would love not to touch the core files.

  2. I just changed in Inchoo_QuoteItemRule.xml from community to local and moved the Inchoo folder to app/code/local and it worked. Why? I have no idea. 🙂

    Thanks again Alice! Hopefully I won’t get into more trouble now. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics