4

I have a custom payment method, after successful payment; payment page return to a controller where it updates order status and truncate current cart items like this:

$this->quote->load($orders->getQuoteId());

$this->quote->setReservedOrderId(null);
$this->quote->setIsActive(true);
$this->quote->removePayment();
$this->quote->save();

$this->cart->truncate();
$this->cart->saveQuote();

Where $this->quote is object of Magento\Quote\Model\Quote
and $this->cart is object of Magento\Checkout\Model\Cart

The cart is truncating properly, but the cart summery count is still appearing on header, and when I go to view cart, it is showing empty cart with previous order total, as shown in image below

enter image description here

My question is, how can I fully empty my cart data after successful payment?

4
  • do you want to update summary section, right? Commented Apr 30, 2019 at 12:37
  • summery would also the part, I want to clear all data related to cart Commented Apr 30, 2019 at 12:42
  • Did you get a solution? Commented May 2, 2019 at 9:18
  • Not yet, will try your code. I will let you know in a while. Thanks for the answer :) Commented May 2, 2019 at 9:18

2 Answers 2

1

You may try to use checkout session Model to clear quote data (i.e Magento\Checkout\Model\Session.php ).

Update your payment module controller code as follow.

I assume your custom payment module controller file name is MypaymentController.php

classs MypaymentController    extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Checkout\Model\Session
     */
    private $checkoutSession;


      public function __construct(
        ........................
        ........................
        SessionManagerInterface $checkoutSession,
        ........................
        ........................
      ){

        ........................
        $this->checkoutSession = $checkoutSession;
        ........................
      }



    public function execute()
    {
       ........................
       $this->_checkoutSession->clearQuote();
       $this->_checkoutSession->clearStorage();
       $this->_checkoutSession->restoreQuote();
       ........................

    }    

}

** Note:**

If your payment module controller already injected the Checkout Session Model (i.e \Magento\Checkout\Model\Session) then do not reinject Session Model, just try to use the functions below in your controller code block.

$this->_checkoutSession->clearQuote();
$this->_checkoutSession->clearStorage();
$this->_checkoutSession->restoreQuote();
5
  • Thanks for the answer :) I will try this, will let you know Commented Apr 30, 2019 at 12:43
  • Still facing same issue :( Commented Apr 30, 2019 at 12:49
  • if possible could you please provide full code of your payment controller? Commented Apr 30, 2019 at 13:05
  • I have shared main parts, that controller has too many stuff with a lot of "if", I have shared relevant parts Commented Apr 30, 2019 at 16:37
  • 1
    there was a code that was effecting was this $quote->setReservedOrderId(null); $quote->setIsActive(true); $quote->removePayment();$quote->save(); so I have removed it and add your code $this->checkoutSession->clearQuote(); now it's working fine. Thanks :) Commented May 2, 2019 at 12:48
3

You need to do following things.

  1. create sections.xml at the following location with the code.

app\code\Vendor\Extension\etc\frontend\sections.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="module/controller/action">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
</config>

NOTE : replace module/controller/action with your above controller action path.

9
  • I don't get it, did you read the question? just asking, because I think you gave me answer to something else :) Commented Apr 30, 2019 at 16:33
  • @ShoaibMunir Just check answer what I give, your controller action clear the cart but summary section have issue, Magento 2 have facility to update particular section. So try the code. Commented May 1, 2019 at 4:17
  • sure, will try it. I will let you know. Thanks Commented May 1, 2019 at 11:18
  • I have tried your code, but it didn't work for me :( Commented May 2, 2019 at 6:24
  • ok confimm you implement code properly and run all the magento commands after change and then check into private browser mode. Commented May 2, 2019 at 6:26

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.