Вопрос

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?

Это было полезно?

Решение

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

Другие советы

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();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top