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