Milind Daraniya

Magento 2 Apply Coupon Code Programmatically

Published July 2nd, 2023 1 min read

Sometimes we need to apply coupon code programmatically in Magento2.Here in this tutorial, we are going to explain how you can apply coupon code in magento2 programmatically.

You can use dependency injection or object manager to apply coupon code in Magento 2. Here is an example :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); // Get Cart
$couponCode = 'your_coupon_code'; // Add Coupon code here
$quote = $cart->getQuote()->setCouponCode($couponCode)
                                  ->collectTotals()
                                  ->save(); // set coupon code in cart quote

I hope this Magento article helped you to find what you were looking for.