Magento 2 remove coupon code programmatically

Today we talk about how Magento 2 remove a coupon code programmatically. You can get quote data from the checkout cart model. You can remove 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 = "";
return $this->cart->getQuote()->setCouponCode($coupon)->collectTotals()->save();

}
}

 

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!