1

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?

2 Answers 2

2

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

$this->checkoutSession->getQuote()->setCouponCode('')
                            ->collectTotals()
                            ->save();
2
  • so this will remove my applied coupon code? Mar 1, 2017 at 10:12
  • Yes, this will remove your coupon code. Mar 1, 2017 at 10:13
1

Try this.

    public function __construct( 
         \Magento\Checkout\Model\Cart $cart,
         \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     ) { 
         $this->cart = $cart;
         $this->quoteRepository = $quoteRepository;
     }
     public function functionName(){
      $cartQuote = $this->cart->getQuote();
      $oldCouponCode = $cartQuote->getCouponCode();
      $codeLength = strlen($oldCouponCode);
       if($codeLength){     
         $cartQuote->getShippingAddress()->setCollectShippingRates(true);
         $cartQuote->setCouponCode('')->collectTotals();//Removing coupon 
         $this->quoteRepository->save($cartQuote);
    }  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.