5

Is there a way to load all items in cart, only using quote entity_id from quote table in magento 2 database?

  • you want the query or the code that retrieves them? – Marius May 31 '17 at 7:10
  • @Marius i don't want to load it from raw query, i need to load it from collection – simple guy May 31 '17 at 7:12
3

I assume you want to get that in a class.
If not, you should.

Let's say the class name is Vendor\Module\Model\QuoteItems. It should look like this:

<?php 
namespace  Vendor\Module\Model;

class QuoteItems
{
    protected $quoteRepository;
    public function __construct(
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    )
    {
        $this->quoteRepository = $quoteRepository;
    }
    public function getQuoteItems($quoteId)
    {
         try {
             $quote = $this->quoteRepository->get($quoteId);
             $items = $quote->getAllItems();
             return $items;
         } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
             return [];
         }
    }
}

then you can use your class to retrieve the items with the method getQuoteItems.
Or you can use the code inside the method directly, just by injecting an instance of the quote repository in your class.

  • it returns empty array – simple guy May 31 '17 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.