0

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?

0

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

$this->checkoutSession->getQuote()->setCouponCode('')
                            ->collectTotals()
                            ->save();
  • so this will remove my applied coupon code? – Alin Lupoiu Mar 1 '17 at 10:12
  • Yes, this will remove your coupon code. – Khoa TruongDinh Mar 1 '17 at 10:13
0

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, privacy policy and cookie policy

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