Question

I get the quote_item_id like this:

$orderItems = $order->getAllItems();
foreach($orderItems as $orderItem){
    $qouteItemId = $orderItem->getQuoteItemId();
}

I need to load quote item collection using quote_item_id.

Is there a way to do it?

Was it helpful?

Solution

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

and in one of the methods you can do this:

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

OTHER TIPS

I want to improve the accepted answer. Unfortunately the repo tied to QuoteItem model \Magento\Quote\Api\CartItemRepositoryInterface doesn't have a get() method. So, to load a quote item entity from db we have 2 options:

  • use the model factory \Magento\Quote\Model\QuoteFactory (like the accepted answer)
  • use the collection factory \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory

On my magento 2.3.5 the issue with the accepted answer is that $quoteItem->getOptions() is empty! To load quote item with options filled, i need to use the collection like this:

public function __construct(
    ...
    \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $quoteItemCollectionFactory
    ...
) {
    ...
    $this->quoteItemCollectionFactory = $quoteItemCollectionFactory;
    ...
}

// load the quote item
$quoteItemCollection = $this->quoteItemCollectionFactory->create();
$quoteItem           = $quoteItemCollection
    ->addFieldToSelect('*')
    ->addFieldToFilter('item_id', [quote_item_id])
    ->getFirstItem();

// retrieve the quote item options
$quoteItemOptions = $quoteItem->getOptions();

you can load the quote id by injecting the instance of \Magento\Quote\Model\QuoteFactory

protected $quoteFactory;
public function __construct(
  .....
  \Magento\Quote\Model\QuoteFactory $quoteFactory
  ......
){
   $this->quoteFactory = $quoteFactory;
}

$quote = $this->quoteFactory->create()->load($quoteId);
$items = $quote->getAllItems();

you can inject in your class an instance of \Magento\Quote\Model\QuoteFactory.

protected $quoteFactory;

public function __construct(
    ...
    \Magento\Quote\Model\QuoteFactory $quoteFactory,
    ....
) {
    ....
    $this->quoteFactory = $quoteFactory;
    ....
}

Then you can use:

$quote = $this->quoteFactory->create()->load($quoteId);

First load the quote object

 $quote = $this->quoteRepository->getActive($cartId);
  foreach ($quote->getItems() as $item) {    
         $quoteItem = $quote->getItemById($item->getItemId()); 
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top