>How to Set Shopping Cart Price Rule in Magento

How to Set Shopping Cart Price Rule in Magento

3 min read

If you are an online owner or an admin of an online store, you must be need to set up rules for shopping cart price rule weekly/monthly. But, as you know, the default of Magento do not support this feature, it only allows to schedule the rules to run in the defined time; that sometimes makes you feel inconvenient as it requires too much manual work

However, thanks to the cron job, which is the scheduled task itself and can be very useful to automate repetitive tasks, running on the server, the schedule for shopping cart price rule becomes quite quick and easy to do. Now we could write either a complete module with full section management in the backend, or a simple PHP code

Assuming that the rule need to run on your store one day a week (for example Saturday) and self inactive on Sunday.

Step1: Create a PHP file in the root directory of Magento.

First of all, include Mage.php in order to use the default function,; below are commands for the Mage.php:

$mageFilename = " app/Mage.php";
$date = date('Y-m-d H:i:s');
if (!file_exists($mageFilename)) {
    echo $date. " --- ". $mageFilename." was not found". PHP_EOL;
require_once $mageFilename;
// Secondly, assume that the ID shopping cart rule is 10, set a rule like this command:
$rule = Mage::getModel('salesrule/rule')->load(10);
// Then check whether today is Saturday by the structure:
if(date('w') == 6){ 
    // After that, save the log when running Cron job: 
               echo $date." --- activated rule #10". PHP_EOL; 
// Next, do as follows if you want everytime the rule is activated you receive a notification email.
$msg = $date." --- activated rule #10";
         mail("[email protected]","Activated Shopping Cart Price Rule #10",$msg);
        echo $date." --- deactivated rule #10". PHP_EOL;
        $msg = $date." --- deactivated rule #10";

When the rule has been deactivated on Sunday, send an email to notify about that by this guide:

if(date('w') == 0){ mail("[email protected] ","Inactivated Shopping Cart Price Rule 10",$msg); } } } catch (Mage_Core_Exception $e) { echo $date." --- Error". PHP_EOL; echo $e->getMessage(); mail("[email protected]","Error occurred whilde activating/inactivating rule #10",$e->getMessage()); } catch (Exception $e) { echo $date." --- Error". PHP_EOL; echo $e->getMessage(); mail("[email protected] "," Error occurred whilde activating/inactivating rule #10",$e->getMessage()); }

Step 2: Set the Cron job

At first, connect to SSH server.

Then run the ‘’crontab – e’’ command and press key ‘’a’’ again to edit the cron job

Add a new line as below, in which:

/chroot/home/user/html/weekend.php: serverpath – going to the PHP code you have created in Part 1.

/chroot/home/user/html/var/log/weekendcron.log: serverpath – going to the log file you will save

In order to save and exit, press ESC and then enter: wq!

To check if cron job running okay, we need to check weekendcron.log file. If you want to test cron job, you should probably fix temporarily “@daily” to “* * * * *” to log more agile.

In order to test the code we can go straight to weekend.php file on a web browser and see the results printed out directly.

Depending on the specific case that code will be different but basically, it remains checking  the date conditions, so actually, those are not complicated. There are many other setup ideas for shopping cart rules for you to try, for example, running promotions on even days, running promo on the specified day of the week,…

< Previous Post
Next Post >