Magento 2 : How to load quote by quote id
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
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 (whereis_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')->loadByIdWithoutStore($quoteId);