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!

  • From where you are getting $cartId? And where does you added this code? – Sukumar Gorai Jun 21 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 at 12:27
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");
}
  • yes, it works. Thank you :) – vDrews Jun 21 at 12:20
0

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

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

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.