8

How can I load quote in magento 2 by quote id ?

14

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

This should work for now, but soon, the load method is going to go away and you need to use a service contract.
So you can use \Magento\Quote\Api\CartRepositoryInterface.
Same as above, inject an instance of this class in your class:

protected $quoteRepository;

public function __construct(
    ...
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
    ....
) {
    ....
    $this->quoteRepository = $quoteRepository;
    ....
}

and use this:

$this->quoteRepository->get($quoteId);

If you want to see how the code looks like, the implementation for \Magento\Quote\Api\CartRepositoryInterface is \Magento\Quote\Model\QuoteRepository

  • Hi @Marius, will this work in case of multiple store views as well? – manini Jul 16 '18 at 7:35
  • I think it will work – Marius Jul 16 '18 at 9:37
11

First you need to inject a \Magento\Quote\Model\QuoteFactory in your class constructor:

protected $_quoteFactory;

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

Then in your class you can do:

$this->_quoteFactory->create()->loadByIdWithoutStore($quoteId);

On a side note you can also use the following methods to load a quote:

  • loadActive($quoteId) where it loads corresponding active quote (where is_active = 1)
  • loadByCustomerId($customerId) where it load the active quote corresponding to the customer id.

NB: you can also use the object manager directly to do it but it is not recommended:

$this->_objectManager->create('Magento\Quote\Model\Quote')->loadByIdWithoutStor‌​e($quoteId);
  • 1
    \Magento\Quote\Model\Quote is a non-injectable class. I mean you can inject it, but it's not the best idea to do so. If you inject this class in 2 other classes you will get it as a singleton in the DI container and if you call load once in one of the classes, you will have it "loaded" in your other class also. Most probably you don't want that. Use factories instead. – Marius May 12 '16 at 14:18
  • @Marius here's what happens when you're working on M1 and M2 projects at the same time. You tend to mix up everything. Thanks for the enlight and +1 for your solution, definitely the right way to go with service contracts – Raphael at Digital Pianism May 12 '16 at 14:22
  • Instead of $this->_objectManager->get('Magento\Quote\Model\QuoteFactory')->create()->loadByIdWithoutStore($quoteId); you can use $this->_objectManager->create('Magento\Quote\Model\Quote')->loadByIdWithoutStore($quoteId); For the same reason stated above. calling get on the OM will result in a singleton. – Marius May 12 '16 at 14:23
  • @Marius I find that get vs create is more obvious than the use of singletons of M1 but I still tend to misuse them – Raphael at Digital Pianism May 12 '16 at 14:25
  • Don't worry. Been there, done that. :) – Marius May 12 '16 at 14:28

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.