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


2 Answers 2


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:


You can try this below code :

$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();


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.