0

I am trying to get the Lastly added Quote Item using "checkout/session".

By I don't know, how to get the last added Item. Hence I have product_id.

I believe instead of loading the collection and all, there would be some function. Can anyone help me out?

1
  • Not lastly added product, but lastly added quote item. Commented May 16, 2019 at 13:10

3 Answers 3

1

try with this. you can use getItemByProduct() method to get quote item from quote using product.

/** @var \Magento\Quote\Model\Quote\Item $item */
$item = $this->_checkoutSession->getQuote()->getItemByProduct($product); // pass the product object

$itemId = $item->getId();
1
  • I expect something else this. Unfortunately, in my case, it doesn't help. Anyway Thanks. Commented May 22, 2019 at 5:56
0

You can get last quoteId from checkout session...then load quote model to get quote item match with product_id what you want from quote id

protected $checkoutSession;

public function __construct(
     \Magento\Checkout\Model\Session $checkoutSession
){
     $this->checkoutSession = $checkoutSession;
}
public function execute(){
     $quoteId = $this->checkoutSession->getLastQuoteId();
}
0

/** Do not use checkoutSession->getQuote()!!! it will cause infinite loop for quotes with trigger_recollect = 1, see Quote::_afterLoad() */

    $items = $request->getAllItems();
    if (empty($items)) {
        return false;
    }

    /** @var \Magento\Quote\Model\Quote\Item $firstItem */
    $firstItem = reset($items);
    if (!$firstItem) {
        return false;
    }

    $quote = $firstItem->getQuote();
    if (!($quote instanceof \Magento\Quote\Model\Quote)) {
        return false;
    }

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.