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
  • Thanks 1

Share this post


Link to post
Share on other sites

Thank you!! (4 years later 😂). There is a little change you have to do if you use newer versions of prestashop, replace:

$coupon = new Discount();

With:

$coupon = new CartRule();

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

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