Magento2
How to load quote by quote id in Magento 2

In Magento 2 you can load Quote by Dependency Injection and by ObjectManager. My preferred method is Dependency Injection, but you can also do it by ObjectManager.Try to avoid the ObjectManager method because it is not a good practice for learning. But in given below examples you can see in both method.

By Dependency Injection

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

namespace Awa\Demo\Controller\Adminhtml\Quote;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;

class Demo extends \Magento\Backend\App\Action
{
     protected $quoteFactory;

     public function __construct(
          Context $context,
          \ \Magento\Quote\Model\QuoteFactory $quoteFactory
     ) {
         $this->quoteFactory = $quoteFactory;
         parent::__construct($context);
    }
   public function execute()
   {
      $quoteId=12;
      $quote = $this->quoteFactory->create()->load($quoteId);
   }
}

By ObjectManager

Try to Avoid this Method.

$quoteId = 12;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager ->create('Magento\Quote\Model\Quote')->loadByIdWithoutStor‌​e($quoteId);

Run php bin/magento setup:di:compile when you add any dependency injection.Because without php bin/magento setup:di:compile it throws error in code.

Hope this will helps you!

Leave a Reply

Your email address will not be published. Required fields are marked *