Question

I used to load the quote like this:

$quote = $this->quoteModel->load($quoteId);

But now I get a warning in PhpStorm saying that this method is deprecated:

Deprecated: 100.1.0 because entities must not be responsible for their own loading. Service contracts should persist entities. Use resource model "load" or collections to implement service contract model loading operations.

So I tried it like this:

use \Magento\Quote\Model\Quote;
use \Magento\Quote\Model\ResourceModel\Quote as QuoteResourceModel;

...

/**
 * @var \Magento\Quote\Model\Quote
 */
protected $quoteModel;

/**
 * @var \Magento\Quote\Model\ResourceModel\Quote
 */
protected $quoteResourceModel;

...

public function __construct(
    Quote $quoteModel,
    QuoteResourceModel $quoteResourceModel
) {
    $this->quoteModel = $quoteModel;
    $this->quoteResourceModel = $quoteResourceModel;
}

...

$quote = $this->quoteResourceModel->load($this->quoteModel, $quoteId);

$logger->info($quote->getId());  // empty

But $quote->getId() returns nothing, so it failed.

Another Attempt:

use \Magento\Quote\Model\QuoteFactory as Quote;
use \Magento\Quote\Api\CartRepositoryInterface;

...

protected $quoteModel;
protected $cri;

...

public function __construct(
    Quote $quoteModel,
    QuoteResourceModel $quoteResourceModel
) {
    $this->quoteModel = $quoteModel;
    $this->cri = $cri;
}

...

$q = $this->quoteModel->create();
$quote = $this->cri->load($q, $quoteId);

$logger->info($quote->getId());  // empty
Was it helpful?

Solution

You need to use CartRepositoryInterface from Magento/Quote

OTHER TIPS

I solved it by applying the informations from @Matias answer:

He said that we should always use interface methods instead of the concrete implementation.

Solution:

use \Magento\Quote\Model\QuoteFactory as Quote;
use \Magento\Quote\Api\CartRepositoryInterface as CRI;

...

protected $quoteModel;
protected $cri;

...

public function __construct(
    Quote $quoteModel,
    CRI $quoteResourceModel
) {
    $this->quoteModel = $quoteModel;
    $this->cri = $cri;
}

...

$quote = $this->cri->get($quoteId);
$logger->info($quote->getId());
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top