I want to get an existing quote through a cron job, but it returns null.

Want get through this $this->quoteFactory->create()->loadByCustomer($custoemrId)



It works without cron job but I need through a cron job.

$checkCartExist = $this->quoteFactory->create()->loadByCustomer($customerId);
  var_dump($checkCartExist->getData()); // returns 0 through a cron

 if (!$checkCartExist['entity_id']) {
      $cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
      $quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote

 } else {

      $quote = $this->cartRepositoryInterface->getForCustomer($customerId);

Cron works because each time it created a new quote item.


You can give a try with below code in your cron file for getting quote items with customer id.

protected $quoteFactory;

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

public function getQuoteCollection($customerId)
    $quote = $this->quoteFactory->create()->getCollection()->addFieldToFilter('customer_id',$customerId);
    return $quote;

Hope it helps!!!

  • Sumit, thanks for answer, $customerQuote->getAllItems() getAllItems method is not defined
    – Robinio
    Sep 20 '19 at 7:16
  • Updated my answer for getting the quote of the customer with customer id.
    – Sumit
    Sep 20 '19 at 7:31

Instead using Quote factory, use quote RepositoryrepMagento\Quote\Api\CartRepositoryInterface::getForCustomer

This method needs two param $customerId, array $sharedStoreIds = []

So you have to provide those two parameters

* Where $this->quote is instance of Magento\Quote\Api\CartRepositoryInterface

$this->quote->getForCustomer({CustomerId}, [{MyStoreId}]);
  • Not get quote using cron. M2.4 Aug 11 '21 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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