Magento2 – How to load Quote by Quote id

Magento2 – How to load Quote by Quote id

Use \Magento\Quote\Api\CartRepositoryInterface Repository as a dependency to create its instance and load the quote using id.

 
private $repository;

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

.... 

$quote = $this->repository->get($quoteId);
.... 

Alternatively you can also get quote using \Magento\Quote\Model\QuoteFactory model. But this has been deprecated, since entities are not responsible to load their own data.

To load quote data using \Magento\Quote\Model\QuoteFactory model, Use the code below:

......
private $quoteFactory;

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

To use it you can write:

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


\Magento\Quote\Model\QuoteFactory model loads only current store specific quote. While using \Magento\Quote\Api\CartRepositoryInterface you can load any store cart data.

If you want to load specific store quote data using \Magento\Quote\Model\QuoteFactory model, you will have to specify the store id before calling load method.

$q = $this->quoteFactory->create()->setStoreId($storeId)
              ->load($quoteId);

2 Comments

  • Hi , Thats great brother 🙂

Leave a Reply

Your email address will not be published.