Time Dependent Shopping Cart Rules in Magento [Freebie]

Magento is probably the most powerful shopping cart currently available on the eCommerce market. In addition, it offers a free access to the core files and developers can add new features due to their needs. At the same time, this procedure requires the high level of technical skills. So, let’s take a deeper look at the process of adding a new feature to Magento. Below you will find out the path of creating a Time Dependent Shopping Cart Rules Extension for this shopping cart.

What Time Dependent Shopping cart Rules Mean?

When you start a promo campaign on your store, it is likely to get as much from the shopping cart as possible. Out-of-box, Magento provides store owners with a limited functionality regarding price rules. You can set only the Start and End Date, but have no permission to configure the exact time. So, Orange 35 Developers Team decided to change this situation. So, after a laborious inspection of Magento core files, the solution was found.

Today, it is available as a Free Magento Extension from Orange35 Store and Magento Connect. You can simply install it and benefit from new features. But, in this article, the development process is described step-by-step. So, keep reading to find out what was done before the release.

Step-By-Step Guide for Magento Extension Development

First of all, we are going to override some of the Magento core models and controllers. To do so, we will basically need to create an extension. So please see some basic steps of creating an extension.

Now we are going to go to the database and check where Magento saves shopping cart price rules. It is ‘salesrule’ table. And as you probably noticed, “from_date” and “to_date” columns have the “DATE” type.
We need to change it to “DATETIME”. Thus, you basically just need to execute this SQL script:

If we check the JavaScript plugin Magento uses to pick the date, we will notice that it already has the time function and Magento itself already has the possibility to turn it on.
To do that we will need to override _prepareForm() function in the Block Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main. We’re interested in these lines:

add ‘time’ => true to each field to enable time inside javascript datepicker

Instead of overriding this block, we will use Magento event/observer feature and we need to put the observer on event named ‘adminhtml_promo_quote_edit_tab_main_prepare_form’. We need to add these lines to our config.xml file:

But we can’t do this overriding a class Mage_Rule_Model_Abstract because the class is abstract. We need to override this function in all Model classes used in Sales Rules that extend this class. So here is the code that needs to be added to <models> tag inside config.xml:

Unfortunately there’s no solution for catalog price rules yet as we didn’t have a chance to go over it.
The catalog price rules system is actually a lot more complicated in terms of how items are being saved and updated. There’s a chance our team will investigate this in the future, but it is not scheduled at the moment.