magento 2 apply coupon code programmatically

Today we talk about how Magento 2 Magento 2 apply a coupon code programmatically. You can get quote data from the checkout cart model. You can set the coupon on the current cart. So let’s start with our example.

namespace QaisarSatti/HelloWorld/Block;

use Magento\Backend\App\Action;

class ApplyCoupon extends Action
{

protected $cart;

public function __construct(
\Magento\Checkout\Model\Cart $cart
)
{
$this->cart = $cart;
}

public function applyCoupon()
{

$coupon = "abc1234";
return $this->cart->getQuote()->setCouponCode($coupon)->collectTotals()->save();

}
}

 

Search

Recent Blogs

Categories

Archives

Archives

Tags