I would like to create a shopping cart price rule which

  • Checks for a pre-defined customer attribute
  • Applies a price rule to the most expensive product in the cart

What's a good starting place for this? What events could I observe? What's the fastest way to implement this?


This is actually not that difficult.

Have a look at Mage_SalesRule_Model_Rule_Condition_Combine::getNewChildSelectOptions

You will see it builds an array of conditions as such:

$conditions = array_merge_recursive($conditions, array(
            array('value'=>'salesrule/rule_condition_product_found', 'label'=>Mage::helper('salesrule')->__('Product attribute combination')),
            array('value'=>'salesrule/rule_condition_product_subselect', 'label'=>Mage::helper('salesrule')->__('Products subselection')),
            array('value'=>'salesrule/rule_condition_combine', 'label'=>Mage::helper('salesrule')->__('Conditions combination')),
            array('label'=>Mage::helper('salesrule')->__('Cart Attribute'), 'value'=>$attributes),

and then fires an event, passing along an object called as 'additional'

Mage::dispatchEvent('salesrule_rule_condition_combine', array('additional' => $additional));

it expects back $additional->getConditions()

so, if you observer that event, you can populate the additional object with your own array of conditions.

In your observer some code like this will do:

$conditions = array(
            array('value'=>'YOURMODULE_NAMESPACE/rule_condition_Mostexpensive', 'label'=>Mage::helper('YOURMODULE_NAMESPACE')->__('Most Expensive Product')),

Now your rule option is cleanly injected into the list of available rules.

The next step is to go create the model it will use. As can be seen from the code above, you will need to create the class


What that would extend is really up to the functionality you intend to use. If for example you intend to use product attributes in the validation, you would extend Mage_SalesRule_Model_Rule_Condition_Product so you can inherit all the functionality surrounding product validation.

In your class, these methods are the important ones. Try not to extend methods if you don't need to, it is best to try re-use core code, which results in a lot more stability of your module.

asHtml() : This holds the options that will be displayed when your rule is selected.

validate : This is where you'd place your logic to determine if the rule is valid. In the end it would simply result in bool.

If you'd like to build a rule that has sub-rules, you'd extend the _combined salesrule classes, as for example: Mage_SalesRule_Model_Rule_Condition_Product_Found

Look at the core code in sales rules. It has all the examples you need. To really understand what is going on, I highly suggest you used a debugger (I use xdebug) with an ide/editor that supports line-breaks. (I use netbeans) This way you can put breakpoints in the salsrule classes and follow the code flow as the rules are built. Make sit a lot easier.

Hope this helps.

|improve this answer|||||
  • so would I make an empty Shopping Cart Rule and then code this up? Or does it apply whether or not a Shopping Cart Rule is present, or too all Shopping Cart Rules? – blisstdev Oct 31 '14 at 3:11
  • or wait! This is going to be visible on the Admin isn't it! Even my boss will be able to turn it on and off? – blisstdev Oct 31 '14 at 3:18
  • The rule will become part of the cart rules available rules. Same place as where you can select current cart rules. This then also means you can use normal cart rule in combination with wyour custom rule (lets says for example cart subtotal > amount) – ProxiBlue Oct 31 '14 at 3:19
  • 1
    YES. so great. you are the best. such a great answer! I will also take your recommendation for using an IDE. P.S. for others out there, I found this today which really helps as for debugging as well: github.com/broderboy/magento-callstack – blisstdev Oct 31 '14 at 3:24
  • I wanted to chime in because this answer really helped me figure out how to set customer attributes as a condition. I wrote a post showing some examples here: peakcollective.com/…. I hope it helps others! – thindery Jul 11 '18 at 14:57

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.