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.

Magento Shopping Cart Price Rule

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

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“.

Shopping Cart Price Rule Condition

Shopping Cart Price Rule Condition

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

app/etc/modules/Inchoo_QuoteItemRule.xml:

<?xml version="1.0"?>
<config>
<modules>
<Inchoo_QuoteItemRule>
<active>true</active>
<codePool>community</codePool>
</Inchoo_QuoteItemRule>
</modules>
</config>

app/code/community/Inchoo/QuoteItemRule/etc/config.xml:

<?xml version="1.0"?>
<config>
<modules>
<Inchoo_QuoteItemRule>
<version>1.0.0.0</version>
</Inchoo_QuoteItemRule>
</modules>
<global>
<models>
<salesrule>
<rewrite>
<rule_condition_product>Inchoo_QuoteItemRule_Model_SalesRule_Rule_Condition_Product</rule_condition_product>
</rewrite>
</salesrule>
</models>
</global>
</config>

app/code/community/Inchoo/QuoteItemRule/Model/SalesRule/Rule/Condition/Product.php:

<?php
 
class Inchoo_QuoteItemRule_Model_SalesRule_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');
 
/* @inchoo */
$attributes['quote_item_sku'] = Mage::helper('salesrule')->__('SKU');
/* @inchoo */
}
 
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())
->setQuoteItemRowTotal($object->getBaseRowTotal())
/* @inchoo */
->setQuoteItemSku($object->getSku())
/* @inchoo */;
 
return parent::validate($product);
}
}

We basically just needed to add two lines of code for the whole thing to work (you can see those two lines surrounded by “/* @inchoo */” comment). 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.

scr2

scr1

Hope it helps.

P.S. This example was provided in its simplest form, mostly just to demonstrate a quick “fix” for product custom options rule. More robust solution would be more proper for serious projects.

You made it all the way down here so you must have enjoyed this post! You may also like:

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

Get rewritten product url in a different store Petar Sambolek
Petar Sambolek, | 9

Get rewritten product url in a different store

34 comments

  1. I have made this with new attributes on sales_flat_quote_item, the options are listed on sales price rules but they does not work… i tested several combination rules and the rule its always invalid…

  2. is there any option in magento 1.9.x to make a sale/promotion for every second/third (every Nth) item ?
    for example 2 items for 10$ or 3 items for 12$

  3. anyone have this working in 1.9.3 ? It caused my promotion rules setting to be unaccessible, the tabs were there but none of the settings. I had already created the rule and it actually worked but I was not able to access my rule settings anymore and had to delete the the inchoo files to get them back.

    1. I fixed this, just ran thew the steps again and it worked properly. I love this additional feature, so much more flexible. Thank you

  4. I have a question that How can I set up shopping cart price rule for each store view? Because Magento default just allows setting this rule at the level of website and apply for all store views.
    I know there are extensions that can help to do it but is there any other ways to do it?

  5. Can we set a condition that some products/categories are not allowed to ship to specific countries?

  6. Good information , but i want to compare the two attribute i have a attribute name “maprice” and want to compare it with the price in promo rule. by default magento it is not possible
    my aim is like is maprice > price so promo is apply otherwise not
    can you please help me to fix it?

  7. product main SKU + “-” + first custom option SKU

    it is possible to without “product main SKU”.

  8. Thanks! You’re genius. Tried many suggested way from other sites but none worked except this. I use 1.8. Thank you!

  9. If u know Can u plz explain me , how to set coupon particular category products if custom option sku is xxxxxx.

    Thanks,

  10. I’ve simple products in my magento store with custom options that have different prices. Upon selecting one of the given options e.g size, the total price doesn’t change and stay the same. However if items purchased in shopping cart it shows correct price (base price+additional amount) I presume there’s some javascript issue in my custom theme as when i changed the template to default it started to work normal. Any ideas what could be the possible issue and where should i be looking to sort this issue out.

  11. Johnboy is right in its second comment even if it seems very strange to extend CatalogRule class for a sales rule …
    In the core, you can see that Mage_CatalogRule_Model_Rule_Condition_Product class extends Mage_Rule_Model_Condition_Abstract (and not Mage_Rule_Model_Condition_Product_Abstract as says Inchoo this class doesn’t exist) but it is not the case of Mage_SalesRule_Model_Rule_Condition_Product which itself extends Mage_CatalogRule_Model_Rule_Condition_Product.
    Note that if you extend Mage_Rule_Model_Condition_Abstract, it works only in the frontend

  12. Hello what i meant was that how do you add the custom option that you need for the shopping cart rule? will it be like SKU_customoptionname_valueofcustomoption?

    Thanks!

  13. You need to go into your attributes “manage attributes” and find the SKU attribute and change it so that it can be viewed in the promotional part.

    Catalog >> Attributes >> Manage Attributes

    click on “sku”

    Then under “Frontend Properties” make sure “use for Promo Rules Conditions” is set to Yes.

    I hope that helps.

    Bradley

  14. Has this worked for anyone? It isn’t clear on how to add the custom options on the SKU option. This would be a big help. Thanks in advance!

  15. The code says to extend Mage_Rule_Model_Condition_Product_Abstract but it as that doesn’t exist it seems it should be Mage_SalesRule_Model_Rule_Condition_Product

  16. Best Tutorial ever! It’s worth to mention, that this also allows for: sku CONTAINS keyword. That is more important to me than: sku IS keyword.
    E.g., so i can give a discount on every product that has “choco” in its sku, regardless of which product was chosen.

  17. I’m not sure if you could help with this, it’s 1.7.02 but its driving me nuts, i’ve setup a promotion, all is working well, and saves, however when i go to the admin, shopping cart promotions, it won’t display, its completely empty in the admin. the page loads, but it doesnt list anything.

  18. Your code never ever work for me 🙁
    Always create files according to your instruction but no luck.

    Can you provide a zip file of code to donwload plz.

  19. Is there any way to use this formula or this module and make it check for customer ID?

    I would like to use the shopping cart rules and check for specific customer ID’s that I will manually input.

  20. Do you think this could work with taxes as well? I have this situation where if the product itself is < $110 no tax apply, if the combination of the products with price < $110 exceed $110 the tax is 4% otherwise 8%

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.