Jump to content

Create new CartRule programmatically and make it incompatible with all the existing ones


univers3

Recommended Posts

Hi there,

I have just made a script to create a new CartRule programmatically.

The problem is that I don't know how to make it incompatible to all the existing ones.

 

This is mi script:

$coupon = new CartRule();

        //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=>"Voucher ".$_GET['valore']."€",2=>"Voucher ".$_GET['valore']."€");

        //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 = "2019-08-21";

        //Make a random code
        $exist = true;
        while ($exist){
                $coupon->code = strtoupper("FNZ-".Tools::passwdGen(5));
                $exist = $coupon->cartRuleExists($coupon->code);
        }

        //Partial Use
        $coupon->partial_use = 0;
        $coupon->reduction_tax = 1;

        //set value of voucher
        $coupon->reduction_amount = $_GET['valore'];

        //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();

The only function in the CartRule class that seems interesting is:

public static function copyConditions($id_cart_rule_source, $id_cart_rule_destination)

 

The problem is that will copy only the existing conditions of another CartRule, if in future I will add more Vouchers they will not incompatibles.

 

Anyone can help me o have the solution to make a CartRule incompatible with ALL EXISTING cart rules?

Thank you very much.

Link to comment
Share on other sites

If I understand correctly, you want to make it so the cart rule cannot be used with any other cart rules? Try setting $coupon->cart_rule_restriction to 0. If that doesn't work, try setting it to 1. Reading the code, I'm having trouble understanding which is for "Cumulable" and which is for "Not cumulable", so you'll have to test and let us know. ;)

  • Like 2
Link to comment
Share on other sites

If I understand correctly, you want to make it so the cart rule cannot be used with any other cart rules? Try setting $coupon->cart_rule_restriction to 0. If that doesn't work, try setting it to 1. Reading the code, I'm having trouble understanding which is for "Cumulable" and which is for "Not cumulable", so you'll have to test and let us know. ;)

 

Yeah you understood the problem! :D

 

And you are right too!

After setting the $coupon->cart_rule_restriction to 1 the new CartRule will be incompatible with any other existing cart rule in the shop.

 

Thank you very much Rocky!

Link to comment
Share on other sites

  • 4 years later...

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
×
×
  • Create New...