Is there any way to add an attribute to a Shopping Cart Price Rule in Magento? I need to add a field called "featured" for every Shopping Cart Price Rule and after that, be able to filter rules by this attribute.
Magento Enterprise – Add Attribute to Shopping Cart Price Rule Coupon
magento-enterpriseshopping-cart-price-rules
Related Solutions
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.
I post a solution here: https://stackoverflow.com/questions/11718504/catalog-price-rule-vs-shopping-cart-rule/34099115#34099115 or here: Disable catalog price rule on coupon code
Catalog rules and Shopping cart rules are treated separately, so they can stop in same group but bot between those two types. So what to do to prevent them stacking?
The idea came from Marshall Thornton' blog: http://www.magentotips.info/modules/catalog-shopping-cart-pricing-rules-stacking/#.VmIev98rKV4
Basicaly you need to extend Mage_SalesRule_Model_Validator::process() method
Best Answer
I ended up using musicliftsme answer. Here it is the complete code with an installer to update the salesrule table.
Module available here.
app/code/local/MB/Attributes/Block/Adminhtml/Edit/Tab/Main.php
app/code/local/MB/Attributes/etc/config.xml
app/code/local/MB/Attributes/sql/mbattributes_setup/mysql4-install-0.1.1.php
app/etc/modules/MB_Attributes.xml
Additional info: When I tried to remove some of the methods from MB_Attributes_Block_Adminhtml_Edit_Tab_Main I got