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. Commented Aug 12, 2021 at 4:24
  • 1
    Fixed using Magento\Store\Model\App\Emulation. Commented Aug 12, 2021 at 5:12
  • okay. But, without Emulation also it's return perfect output. I checked. Commented Aug 12, 2021 at 5:30
  • then post an as an answer. so other also know that. Commented 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. Commented Aug 12, 2021 at 6:30
  • It's basic code. You can add filter and other points based on your requirements. Commented Aug 12, 2021 at 6:38
  • 1
    Thank you...!!! Commented 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 you have read our privacy policy.

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