Magento – Magento 2.2 Programatically apply coupon code

couponcoupon-codesmagento2shopping-cart-price-rules

How can i apply coupon code programatically in magento 2 using cart id.

Thanks.

Best Answer

I believe checkout session can be used instead cartId use below code:

Two steps:

Step 1: Define \Magento\Checkout\Model\Session in your constructor

Step 2:

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