I am using RESTful APIs of Magento and I want to get the last added product id of in the cart. All of my searches are using the Checkout Session method of Magento which is


But this is not applicable on my end since I am using the RESTful APIs. Can someone help me if there are any available functionality where I can put a one liner method and class to get this with Quote. My other solution is to fetch by Quote :: getAllVisibleItems() and get the last item's product id but I believe it would affect the performance.

Thank you for your help folks.

  • try below my code in answer's section. – Balwant Singh Aug 5 '19 at 10:06

Try below code

public $cart;

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

public function getLastQuoteProduct(){
    $itemsCollection = $this->cart->getQuote()->getItemsCollection();
    $itemsCollection->getSelect()->order('created_at DESC');
    $latestItem      = $itemsCollection->getLastItem(); 
    $product         = $latestItem->getProduct();
    echo 'ID: '.$latestItem->getProductId().'<br />';
    echo 'Name: '.$latestItem->getName().'<br />';
    echo 'Sku: '.$latestItem->getSku().'<br />';
    echo 'Quantity: '.$latestItem->getQty().'<br />';
    echo 'Price: '.$latestItem->getPrice().'<br />';
    echo "<br />";

I hope this will work for you...!!!

| improve this answer | |
  • Thanks bro. I will try and see – Brandon Orphiano Aug 5 '19 at 10:15
  • @BrandonOrphiano : please let me know if this solution work for you. happy coding :) – Balwant Singh Aug 5 '19 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.