Jump to content

How to apply coupon code programatically for API


TheLousyCoder

Recommended Posts

Hi All,

 

I am developing APIs for mobile app. I am stuck at the point where I want to apply coupon code on the cart.

I want to know how If there is any function calling which will return me the discounts applicable on coupon.

 

 

Any help will greatly appreciated.

 

 

Regards

Link to comment
Share on other sites

Hi, if you want to create the discount, you can do it by creating an object cartRules:

 

$cart_rule = new CartRule();

//just change the value by what you want:

$cart_rule->name = name (should be an array of every language names with the id_lang as keys);
$cart_rule->id_customer = 0;
$cart_rule->date_from = date('Y-m-d H:i:s');
$cart_rule->date_to = date('Y-m-d H:i:s');
$cart_rule->description = discount_description;
$cart_rule->quantity = 1;
$cart_rule->quantity_per_user = 1;
$cart_rule->priority = 1;
$cart_rule->partial_use = 1;
$cart_rule->code = your_code;
$cart_rule->minimum_amount = 0;
$cart_rule->minimum_amount_tax = 0;
$cart_rule->minimum_amount_currency = 1;
$cart_rule->minimum_amount_shipping = 0;
$cart_rule->country_restriction = 0;
$cart_rule->carrier_restriction = 0;
$cart_rule->group_restriction = 0;
$cart_rule->cart_rule_restriction = 0;
$cart_rule->product_restriction = 0;
$cart_rule->shop_restriction = 0;
$cart_rule->free_shipping = 0;
$cart_rule->reduction_percent = your percentage or 0.00;
$cart_rule->reduction_amount = your_amount or 0.00;
$cart_rule->reduction_tax = 1;
$cart_rule->reduction_currency = 1;
$cart_rule->reduction_product = 0;
$cart_rule->gift_product = 0;
$cart_rule->gift_product_attribute = 0;
$cart_rule->highlight = 0;
$cart_rule->active = 1;
$cart_rule->date_add = date('Y-m-d H:i:s');
$cart_rule->date_upd = date('Y-m-d H:i:s');

$cart_rule->add();

when the object is create you can have the key by $cart_rule->id and then add it to the cart:

$this->context->cart->addCartRule($id_cart_rule);
Link to comment
Share on other sites

Thanks fred-vinapresta , this helped me a lot... 

I can successfully apply voucher to the cart. Below is the code which worked for me

 

if (($cartRule = new CartRule(CartRule::getIdByCode($coupon))) && Validate::isLoadedObject($cartRule)) {
            
            $cart = new Cart($cartid);
            $cartresult = $cart->addCartRule($cartRule->id);
            $cartProducts = $cart->getCartRules();
             $cou = array('success' => 'true', 'discount' => $cartProducts[0]['value_real']);
            $coujson = json_encode($cou, true);
}
Link to comment
Share on other sites

  • 2 months later...
  • 6 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...