I want cart to be refilled after customer return back to my controller with some error. Here is my controller code, I can access increment_id here

public function execute()
    if(array_key_exists("req_reference_number",$_POST)) {
        $increment_id = $_POST["req_reference_number"];
        $status = $_POST["decision"];
        $message = $_POST["message"];
        $order = $this->order->loadByIncrementId($increment_id);
        if ($status == "CANCEL") {
            //Cancel previous order

            ** Want to refill cart here

            //redirect to checkout page with restoring cart
            $redirectionUrl = $this->url->getUrl('checkout/cart/');
        $redirectionUrl = $this->url->getUrl('/');

My question is, how can I refill my cart using order increment id that is placed and cancelled.

I had used this code but it's not working, my cart is still empty

$quote = $this->quoteRepository->get($order->getQuoteId());
  • Take a look at the "Reorder" button logic in customer order history. It does exactly what you need.
    – Lorenzo
    Mar 1, 2019 at 6:35

1 Answer 1


You can try to reactivate the old quote, delete payment and reserved order id and reuse it. In that way you preserve also address data. I don't know your context, but with injecting the mentioned object the approach should work.

//inject cart model and checkout session model to your controller
protected $cart;
protected $checkoutSession;

public function __construct(
     \Magento\Checkout\Model\Cart $cart,
     \Magento\Checkout\Model\Session $checkoutSession
    $this->cart = $cart;
    $this->checkoutSession = $checkoutSession;

$quote = $objectManager->create('Magento\Quote\Model\QuoteFactory')->create()->load($order->getQuoteId());


//replace the quote to the checkout session (I guess this is the better way)

//OR add quote to cart

//if your last order is still in the session (getLastRealOrder() returns order data) you can achieve what you need with this one line without loading the order:
  • Tried your code, but my cart is still empty :( Mar 1, 2019 at 9:20
  • Load your cart model and add the quote $cart->setQuote($quote).
    – HelgeB
    Mar 1, 2019 at 9:22
  • I have added the code that I was using to fill up cart. please have a look in question. I highly appreciate your support Mar 1, 2019 at 9:22
  • OK, I have updated the code try adding the quote to the checkout session first since the cart is taking the quote from there
    – HelgeB
    Mar 1, 2019 at 9:40
  • 1
    Sorry, I've updated the code above, the correct method in the checkout session is replaceQuote, I had a mistake in my code
    – HelgeB
    Mar 1, 2019 at 10:31

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.