Custom Shopping Cart Price Rule for Most Expensive Product

customer-attributeshopping-cart-price-rules

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?

Best Answer

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

YOURMODULE_NAMESPACE_Model_Rule_Condition_Mostexpensive 

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.

Related Topic