Jump to content
Stéphane1405

[TIP] Creating cart rule programmatically

Recommended Posts

The following script will create a new cart rule that can for instance be used for coupon code (voucher).

 

This is drawn from cart rule object model definition (classes/CartRule.php L78).

$coupon = new Discount();

//put here the coupon name + translations
//ex: if your website is in french (id language = 1) and english (id language = 2)
$coupon->name = array(1=>"mon coupon",2=>"my coupon");

//by default cart rule is valid for one client that can use it one time

//validity
$coupon->date_from = "2015-08-20";
$coupon->date_to = "2015-08-21";

//code
$coupon->code = "789456";

//50 % off
$coupon->reduction_percent = "50";

//conditions
//ex: rule only applies if the order is 100 or more including taxes and exluding shipping
$coupon->minimum_amount = 100;
$coupon->minimum_amount_tax = 1;
$coupon->minimum_amount_currency = 1;
$coupon->minimum_amount_shipping = 0;


//this creates the coupon
$coupon->add();

Hope this will help
 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More