1

I am able to get my last order thanks to this code:

    $collection = $this->collection->create();
        $collection->addFieldToFilter('customer_id', $customerId);
//        echo "<pre>".print_r($collection->getLastItem()->getData(), true)."</pre>"; //show data of last quote
        $quoteId = $collection->getLastItem()->getData('entity_id'); //get id of last quote
        echo "quote id : " . $quoteId;

        $quote = $this->_quoteRepository->get($quoteId);
        $products = $quote->getItemsCollection()->getData();
//        echo "<pre>".print_r($products, true)."</pre>"; //show data of last quote

        $productsRenew = array();
        foreach($products as $product){
            $productsRenew[] = array(
              'product_id' => $product['product_id'],
                'qty' => $product['qty'],
                'price' => $product['price'],
                'sku' => $product['sku'],
                'name' => $product['name'],
            );
        }
        echo "<pre>".print_r($productsRenew, true)."</pre>"; //show items data

I also got the products detail, now I want to put products details inside the cart.

I've used this to add product one by one on one on my page:

public function getAddToCartPostParams($product)
{
    return $this->listProductBlock->getAddToCartPostParams($product);
}

Now I want to add several products in a row, but I don't know how to do this.

EDIT : solution :

        $customerId  = $this->_customerSession->getCustomer()->getId();
    $collection = $this->collection->create();
    $collection->addFieldToFilter('customer_id', $customerId);
    $quoteId = $collection->getLastItem()->getData('entity_id'); //get id of last quote

    $quote = $this->_quoteRepository->get($quoteId);
    if ($quoteId) {
        try {
            $quote->setIsActive(1)->setReservedOrderId(null);
            $this->_quoteRepository->save($quote);
        } catch (\Exception $e) {
        }
        $this->_checkoutsession->replaceQuote($quote);
    }

1 Answer 1

1

You can use the following code to reactivate the quote(with products):

$quote = $this->_quoteRepository->get($quoteId);
if ($quote->getId()) {
    $quote->setIsActive(1)->setReservedOrderId(null)->save();
    $this->checkoutSession->replaceQuote($quote);
}

Also you need to inject the class \Magento\Checkout\Model\Session to your contruct function and then you can use the $this->checkoutSession

7
  • Awesome, works perfectly. It's even more simple than what I've done. Great ! Thanks ! Commented Jul 24, 2018 at 10:27
  • I've change 1 little part because save() is deprecated, we need to use quoteRepository->save($quote) Commented Jul 24, 2018 at 10:29
  • 1
    Thats fine. Happy to Help!. Commented Jul 24, 2018 at 10:32
  • This is working perfectly, but if I create a quote, then delete a product and after reload last quote, this isn't working Commented Jul 24, 2018 at 12:10
  • Yes because that is now changed with your new quote. Commented Jul 24, 2018 at 12:15

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.