Magento 2 Apply Coupon Code Programmatically


Magento 2 Apply Coupon Code Programmatically– Sometimes we need to apply coupon code programmatically in Magento2. It is a little bit different than magento 1.x. Here in this tutorial, we are going to explain how you can apply coupon code in magento2 programmatically.


Magento 2 Apply Coupon Code Programmatically Example

Magento 2 Apply Coupon Code Programmatically

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

Magento 2 Apply Coupon Code Programmatically Example:

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

You can apply any custom coupon simply as above example.


Advertisements

Add Comment

📖 Read More