0

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

2 Answers 2

1

You need to use CartRepositoryInterface from Magento/Quote

6
  • There is no model called CartRepositoryInterface in module-quote/Model
    – Black
    Commented Nov 9, 2020 at 15:10
  • 1
    Because it's an interface, \Magento\Quote\Api\CartRepositoryInterface
    – Matías
    Commented Nov 9, 2020 at 15:12
  • 1
    You must use interface, instead of the concrete implementation every time you can
    – Matías
    Commented Nov 9, 2020 at 15:13
  • I tried it, but it still fails, I have updated my question with the code.
    – Black
    Commented Nov 9, 2020 at 15:18
  • You aren't assign $cri variable, and if you look at the CartRepositoryInterface you will noticed that the method you need to use is ->get($quoteId) instead of ->load()
    – Matías
    Commented Nov 9, 2020 at 15:21
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.