5

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?

1
  • do you need quote item Id?
    – MR.R DNATH
    Commented Jul 4, 2018 at 7:28

5 Answers 5

11
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);
1
  • I have try above code for load Quote Item by item id but i am getting following error. "Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Quote\\Model\\ResourceModel\\Quote\\Item::getProductId() Commented Oct 22, 2018 at 11:06
4

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();
1
  • 1
    Thanks, Worked perfectly for me on 2.4.3. Commented Aug 31, 2022 at 16:10
1

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();
2
  • 1
    i need to load quote item using quote item id not load quote by quote id Commented Jul 4, 2018 at 7:30
  • Yes you can get items by $items = $quote->getAllItems(); Commented Jul 4, 2018 at 7:32
-1

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);
1
  • i need to load quote item using quote item id not load quote by quote id Commented Jul 4, 2018 at 7:30
-1

First load the quote object

 $quote = $this->quoteRepository->getActive($cartId);
  foreach ($quote->getItems() as $item) {    
         $quoteItem = $quote->getItemById($item->getItemId()); 
  }

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.