Magento – Magento2: How to get quote item through item id

cart-itemsmagento2quoteitem

I want to get quote item through the item id on the guest page.

Got the null value

<?php
namespace Codism\Csr\Block\Index;

class Approveitem extends \Magento\Framework\View\Element\Template {

protected $_coreRegistry;


public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $coreRegistry,
    \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
    \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel
)
{
   $this->_coreRegistry = $coreRegistry;
   $this->quoteItemFactory = $quoteItemFactory;
   $this->itemResourceModel = $itemResourceModel;
   parent::__construct($context);
}

public function getCustomerCartDetails(){
    $itemId = $this->_coreRegistry->registry('itemId');

    $quoteItem = $this->quoteItemFactory->create();
    $item = $this->itemResourceModel->load($quoteItem, $itemId);

    return $item;
}
}

Best Answer

You can get by following code.

public function __construct(
  \Magento\Quote\Model\Quote\ItemFactory $quoteItemFactory,
  \Magento\Quote\Model\ResourceModel\Quote\Item $itemResourceModel
) {
   $this->quoteItemFactory = $quoteItemFactory;
   $this->itemResourceModel = $itemResourceModel

}

Below is code for your logic

$itemId = your item id
$quoteItem = $this->quoteItemFactory->create();
$this->itemResourceModel->load($quoteItem, $itemId);

This code is given here in answer.

Related Topic