1

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
            $this->statusCanceled($order);

            /*************
            ** Want to refill cart here
            **************/

            //redirect to checkout page with restoring cart
            $redirectionUrl = $this->url->getUrl('checkout/cart/');
            $this->response->create()->setRedirect($redirectionUrl)->sendResponse();
        }
    }else{
        $redirectionUrl = $this->url->getUrl('/');
        $this->response->create()->setRedirect($redirectionUrl)->sendResponse();
    }
    die();
}

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());
$quote->setIsActive(1)->setReservedOrderId(null);
$this->quoteRepository->save($quote);
$this->session->replaceQuote($quote);
1
  • 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

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());

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

//replace the quote to the checkout session (I guess this is the better way)
$this->checkoutSession->replaceQuote($quote);

//OR add quote to cart
$this->cart->setQuote($quote);

//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:
$this->checkoutSession->restoreQuote();
9
  • 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.