2

I want to delete the coupon code programmatically. My code is:

$cart = $this->quoteRepository->getActive($cartId);

  if(isset($response_json['errorMessage']) || $json_response['status']['statusCode'] == 0){
    $cart->setCouponCode('');
    $cart->collectTotals()->save();
    $this->logger->debug("test");
  }

I see the "test" in my debug. But the Coupon code not removed. Can someone help me?

Thank you in advance!

2
  • From where you are getting $cartId? And where does you added this code? Jun 21, 2019 at 12:08
  • I added this code in my plugin and the beforePlaceOrder function. But the answer of Jaimin Sutariya works for me.
    – vDrews
    Jun 21, 2019 at 12:27

2 Answers 2

0

You can do it like below.

if(isset($response_json['errorMessage']) || $json_response['status']['statusCode'] == 0){
    $cart->setCouponCode('');
    $this->quoteRepository->save($cart->collectTotals());
    $this->logger->debug("test");
}
1
  • yes, it works. Thank you :)
    – vDrews
    Jun 21, 2019 at 12:20
-1

I suggest another way. Go to your xml file and put this code at before body tag close

<referenceBlock name="block_name" remove="true"/>
0

Your Answer

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

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