0

Not able to get quote data by customer id using Magento\Quote\Api\CartRepositoryInterface (method getForCustomer) with cron. Also tried with Magento\Quote\Model\Quote with loadByCustomer method.

Working fine without a cron job.

Any solution?

4
  • Upload your full code what you have tried. Aug 12, 2021 at 4:24
  • 1
    Fixed using Magento\Store\Model\App\Emulation. Aug 12, 2021 at 5:12
  • okay. But, without Emulation also it's return perfect output. I checked. Aug 12, 2021 at 5:30
  • then post an as an answer. so other also know that. Aug 12, 2021 at 5:35

2 Answers 2

1

Need to inject in your construct \Magento\Quote\Model\QuoteFactory

<?php

namespace RH\Helloworld\Cron;

class CustomCronQuote
{
    protected $quoteFactory;

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

    public function execute()
    {
        $quote = $this->quoteFactory->create()->getCollection()->addFieldToFilter('customer_id',1); // pass your customer id
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/Rohan.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info(print_r($quote->getData(),true));
    }
}

Checked Magento Version : 2.4.2

3
  • Also, require to add is_active filter, because there are many quotes for one customer in diff stores. Aug 12, 2021 at 6:30
  • It's basic code. You can add filter and other points based on your requirements. Aug 12, 2021 at 6:38
  • 1
    Thank you...!!! Aug 12, 2021 at 6:41
0

Fixed using Emulation,

<?php

namespace Vendor\Module\Model;

use Magento\Store\Model\App\Emulation;

class File
{
    protected $emulation;

    public function __construct(
        Emulation $emulation
    ) {
        $this->emulation = $emulation;
    }

    public function FunctionName()
    {
        $this->emulation->startEnvironmentEmulation(1, \Magento\Framework\App\Area::AREA_FRONTEND, true);
        --- Your Code ---
    }
}

Your Answer

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

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