How to apply an existing coupon code programmatically in Magento 2

I try to apply an existing coupon code programmatically in a controller by coupon id.

The only thing I found these code lines for Magento 1.

Mage::getSingleton('checkout/cart')
        ->getQuote()
        ->setCouponCode($coupon)
        ->collectTotals()
        ->save();

Does anyone know how can I do this in magento 2?

Solutions

Use Checkout model session to get quote data.

 /** @var \Magento\Checkout\Model\Session $checkoutSession**/

$this->checkoutSession->getQuote()->setCouponCode($couponCode)
                                  ->collectTotals()
                                  ->save();

Remember to inject \Magento\Checkout\Model\Session in your constructor.

Take a look: vendor/magento/module-checkout/Controller/Cart/CouponPost.php

Similar questions

Customize "apply coupon" action when a particular coupon is applied and then re-update shipping in cart & checkout
I am running magento ver. 1.9.0.1 in my site. I want to customize shipping rate when a particular coupon is applied. How can I do that when "Apply Coupon" button is clicked, if a given coupon is the one, that is applied ? I never had much encounter with code in price rule section so I am not aware of which controller is executed when "Apply Coupon"...
Remove existing and assign new categories to existing product while updating it programmatically in magento 2
How can I remove existing and assign new categories to product programmatically in magento 2 while updating product data.
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..
New coupon code comes up as already existing
Am trying to create a new coupon code but when I try to save it an error message comes up saying that this code already exists. I need to use this particular code as have already heavily advertised it.
Add coupon code to an existing Item in Admin Panel
I am currently working on adding a discount for an existing order which is not invoiced yet in Magento admin panel. To do that I thought it is better to use Coupon codes. What I am trying to do is adding a coupon code for an existing order to make a discount on that order. Till now I have added the coupon code input box into Order control panel. I ...
Applying a coupon code to an existing order - (without cancelling)
I have a customer that has ordered a product that isn't ready to ship. To compensate the customer, I'd like to offer a discount by applying a coupon code on their pending order. I'm aware that I can cancel the order and then apply the discount, but since this is a credit card transaction, I do not want to cancel the order or obligate the customer t...

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.