Back to blog home

Create custom cart validation rules (developers module)

Every day I have fun working on interesting features, that are not trivial and need some time for research, planning and discussing with teammates to get a different point of view on the problem.

Recently I had to work on a feature that required to create multiple cart validation rules. Each rule had complex algorithms based on the product attributes, cart quantity and some parameters that were filled in custom MySQL table. Basically the algorithm applied validation logic, which means that it says if the validation passed or failed.

If we look at the frontend point of view, when we add product in cart and validation for certain item is not passed, then we show a cart error message, cart item error message and the checkout button is not visible. This is something that Magento does by default if we have any validation errors in the cart. So I had to find a way to add and remove cart errors, the rest (displaying and disabling the checkout) worked out of the box.

For reference I looked at the core, because I wanted to follow the way how the Magento core team does it. I found a nice method in the Mage_CatalogInventory_Model_Observer::checkQuoteItemQty(), which was fired after the event sales_quote_item_qty_set_after... I reviewed this method to get a clue about everything that I need for creating my own validation rules. Maybe I will delve more into details, e.g. how the validation errors can be added and removed another time, because today I want to present something else and it's a module which helps you create your own cart validation rules, without having to wonder about how the things work in the background.

I created an extension allowing you to create cart validation rules by adding some lines in your module config.xml and creating a simple class with three required methods. I needed flexibility because the project was in a state where some requirements were still not clear and I wanted to avoid the situation where I have to change logic in a single file each time we get new or changed requirement.

The module

You can download / clone the module from here:

https://github.com/Jarlssen/Jarlssen_CustomCartValidation

After you have copied the module into your Magento installation the best approach is to create your own module extending the logic of Jarlssen_CustomCartValidation

To use the module you have to follow 3 important steps:

1. Create your own module and add dependency to Jarlssen_CustomCartValidation

<?xml version="1.0"?>
<config>
<modules>
<MyCompany_ExampleValidation>
<active>true</active>
<codePool>local</codePool>
<depends>
<Jarlssen_CustomCartValidation />
</depends>
</MyCompany_ExampleValidation>
</modules>
</config>

2. Add configuration in your module config.xml similar to:

<global>
<jarlssen_custom_cart_validation>
<rules>
<different_manufacturer_not_allowed>
<product_type>simple</product_type>
<model>example_validation/validator_validateAllowedManufacturers</model>
<origin>example_validation</origin>
<code>1</code>
</different_manufacturer_not_allowed>
<multiple_quantity_validation>
<product_type>*</product_type>
<model>example_validation/validator_validateMultipleQty</model>
<origin>example_validation</origin>
<code>2</code>
</multiple_quantity_validation>
</rules>
</jarlssen_custom_cart_validation>
</global>

Let me give more details about the example you need to follow:

  • If you want to add validation rule you need to your rules in global/jarlssen_custom_cart_validation/rules
  • you can use arbitrary names for the rules. In the example I called the first rule "different_manufacturer_not_allowed"
  • There are four required nodes in the rule configuration:
  • product_type - you can define for which products the rule will work or you can use "*" for all products
  • model - this is the path to the validation rule model (this is the place where you implement the validation logic)
  • origin - this can be a unique identifier, but most importantly it should be unique in combination with the code config node
  • code - this must be unique in combination with the origin

 

  3. Create a model class in your module extending 

Jarlssen_CustomCartValidation_Model_Validator_Rule and create 3 methods in this class:

  • validate()
  • getQuoteItemMessage()
  • getQuoteMessage()

Basically the methods listed above are abstract methods in Jarlssen_CustomCartValidation_Model_Validator_Rule, so you are forced to create them, otherwise PHP will throw a fatal error.

The validate() method accepts as parameter a Quote item object and returns a boolean value, it should look like this example:


/**
* Implementation of the validation logic
*
* @param Mage_Sales_Model_Quote_Item $item
* @return bool
*/
public function validate($item)
{
$qty = $item->getQty();
if($qty % 10 != 0) {
return false;
}
return true;
}

 

In this example I check if the quantity of the quote item is a multiple of 10.

The method getQuoteItemMessage() returns the text we usually show under the product name in the cart. Example:


/**
* In case we got validation error we have to specify and error
* message, that will be show in the quote item line
*
* @return string
*/
public function getQuoteItemMessage()
{
return "The quantity must be multiple times of 10";
}

 

The method getQuoteMessage() returns the text we usually show as a general error message in the cart. Example:

/**
* In case we got validation error we have to specify an error
* message, that will be shown in the quote
* (usually shown in the cart heading section or product heading section
*
* @return string
*/
public function getQuoteMessage()
{
return "Not allowed product quantity in the cart";
}

 

This is basically how it works, I also create example module with some extra admin functionality, so you can download it and make your own experiments: https://github.com/ceckoslab/MyCompany_ExampleValidation

Conclusion

The module for adding custom cart validation rules gives you flexibility and makes the developers' life easier because they can focus on working on the validation logic instead of digging deep into the Magento core to see how the core team made their tricks. Of course to make it work you have to spend some time learning the validation rules config, but I hope I made it work as simple as possible.

For sure there are some things which can be improved in the core of Jarlssen_CustomCartValidation, but so far if you use the proper config.xml configuration and the public methods validate(), getQuoteItemMessage() and getQuoteMessage() you won't experience any problems if I make changes to the core of the extension.
Proof reading by Oliver Baumann.
Thanks!