Magento 2: Apply coupon code programmatically

I want to apply coupon code programmatically when the condition is true. If condition false coupon code should be not applied.

Please help me.

Solutions

You can try this below code :

Construct Method :

protected $cart;

public function __construct(
  \Magento\Checkout\Model\Cart $cart
){
   $this->cart = $cart;
}

Then, you can use this below code in your function :

$cart = $this->cart;
$couponCode = 'your_coupon_code';
if(condition true)
{
    $quote = $cart->getQuote()->setCouponCode($couponCode)->collectTotals()->save();
} else {
    $quote = $cart->getQuote()->setCouponCode('')->collectTotals()->save();
}

Object Manager Method :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$couponCode = 'your_coupon_code';
if(condition true)
{
    $quote = $cart->getQuote()->setCouponCode($couponCode)->collectTotals()->save();
} else {
    $quote = $cart->getQuote()->setCouponCode('')->collectTotals()->save();
}

Note : Use construct method instead of object Manager.

Similar questions

Magento Apply Coupon programmatically But Its Printing Values
I am using i can apply coupon perfectly but when this line run then echo coupon in template. i don't know why echo coupon in template..
How to apply only valid coupon codes to shopping cart programmatically?
I have a number of coupon codes which i want to apply to the cart programmatically. I have done it like this: Now what happens is that if the first coupon is valid, it got applied, but after that Now I need help to tackle the situation as how to be able to apply both coupon/rules/discounts if valid (if possible applying two coupon codes)? or not ap...
Validate and apply coupon when create an order programmatically
I create an order programmatically like this: how can i apply coupon code in here and validate the coupon code?
Magento Coupon codes although deleted already, doesn't allow to create coupon with same code again
I am not sure if it's some kind of core-logic(unknown to me) or a bug maybe, but even though I have already deleted a coupon-code rule from "Shopping Cart Price Rules", when I search in the "salesrule_coupon" table the deleted coupon code, it still shows up. What happens due to above scenario is that if I try to recreate the coupon-rule with same c...
What observer need to use for NO-COUPON code apply option in magento?
In magento Shopping cart price rules, I need to customize NO-COUPON code option in store basis.I have completed the coupon code option(Store basis) when we enter the coupon code and submit. Magento have no-coupon code option in which discount amount automatically has been applied. But I don't know how to customize NO-COUPON code option.Where can I ...
Apply Coupon code to QUOTE with external script Magento
i want to apply coupon code from external script, that will set code and discount amount to quote and final result will be saved quote with new total. I have done following but not saving quote.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.