TheLousyCoder Posted March 16, 2015 Share Posted March 16, 2015 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 More sharing options...
fred-vinapresta Posted March 16, 2015 Share Posted March 16, 2015 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 More sharing options...
TheLousyCoder Posted March 17, 2015 Author Share Posted March 17, 2015 Hi fred-vinapresta, Thanks a lot for reply. But I don't want to create a coupon, I want to redeem a coupon. I want to take a coupon code from my app user and apply it on the cart so that discounts are availed. Is there any function in prestashop already that will help me do this or any trick? Link to comment Share on other sites More sharing options...
fred-vinapresta Posted March 17, 2015 Share Posted March 17, 2015 Hi, first you have to retrieve the coupon's id with the code add apply the cart rule. You can do this like that: $cartRule = new CartRule(CartRule::getIdByCode(your_discount_code)); $this->context->cart->addCartRule($cartRule->id); 1 Link to comment Share on other sites More sharing options...
TheLousyCoder Posted March 17, 2015 Author Share Posted March 17, 2015 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 More sharing options...
Alex.Tarasenko Posted May 18, 2015 Share Posted May 18, 2015 (edited) Hello TheLousyCoder, Please share with us where to implement your code.( at OrderOpcController or somewhere else ). P.s. I have found similar code at PaymentModule.php Edited May 19, 2015 by Alex.Tarasenko (see edit history) Link to comment Share on other sites More sharing options...
Nickovitshj Posted July 27, 2021 Share Posted July 27, 2021 How can we specify the underlying parameters for: $cart_rule->product_restriction = 0; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now