Magento 2 – How to Remove Applied Coupon Code in Custom Controller

controllerscoupon-codesdiscountmagento2

I have a custom controller where i apply coupon code discount like this:

$this->checkoutSession->getQuote()->setCouponCode($discount)
                            ->collectTotals()
                            ->save();

Now, like magento 2 default does, I want to be able to remove that coupon code discount.

So i created a custom controller and i want to remove here the discount:

the only thing i found to remove the coupon discount is this:

$couponCode = $this->getRequest()->getParam('remove') == 1
        ? ''
        : trim($this->getRequest()->getParam('coupon_code'));

But this dosen't work for me.

Does anyone know what it's the right way to remove this discounts in my controller?

Best Answer

You should try setCouponCode('') with empty value.

$this->checkoutSession->getQuote()->setCouponCode('')
                            ->collectTotals()
                            ->save();
Related Topic