Generally, the Mage_SalesRule_Model_Rule_Condition_Address model is responsible for validating a customer’s address and adding conditions in the SalesRule.

Say, if you want to create a shipping cart price rule for all the European countries, you need to check whether the country is a part of the EU or not. That can be done by means of rewrite.

I’ve created a module for this task, which will hopefully help you do that in a couple of clicks.

The file that registers the module is the system is located here:  app/etc/modules/MageWorx_EuRules.xml:

The locale file is: app/locale/en_US/MageWorx_EuRules.csv:

And this is the configuring file of the module app/code/community/MageWorx/EuRules/etc/config.xml , where you call the rewrite of the salesrule/rule_condition_address model:

Note that the Helper stays empty. You just need it, as it is one of Magento requirements when implementing an operation of such a type:  app/code/community/MageWorx/EuRules/Helper/Data.php:

And this is the model with the main rewrite:  app/code/community/MageWorx/EuRules/Model/Rule/Condition/Address.php :

 

Below is the explanation of what our modification of the main methods have done:

loadAttributeOptions – now it adds the attribute of the country/ selection of countries. For example, when creating a shopping cart rule for European Union countries, it should be ‘eu‘. This is the code attribute that will be used every time when a customer’s address is passing validation.

getInputType, getValueElementType, getValueSelectOptions – sets the input type as select for a newly created attribute. It lets you use the standard yes/no  model and avoid entering ‘1’ (or something else) when defining the value. Also, this method will save you time on entering extra variables (e.g. equals or greater than).

validate – before calling out the standard validation, our modified validate method fills in a customer address with the required value (eu in our case). In order to obtain it, we use the countries from the Admin panel chosen as counties of the European Union and compare them with the current customer’s country.  If the county is a part of EU, we set into the address the eu value in TRUE.

The condition for this example will look like that:

9c1ffb3eba39156fcb7904ac145bc551
Next, we create he corresponding rule and run the check. In my case customers from the EU get a 10% discount (the rule is named – EU RULE).

The first sceen-shot is for the USA, the second one if for the UK:

03c046a4d79680bb97e533e39cf9e7ec           a845e7aa5c5ea044ec3279bcb1178378

Hope this will help you in creating  custom shopping cart price rule.

Questions? Feel free to add them in the comments section below.