0

How can I get the abandoned cart collection on Magento 2? I want to collect it via controller.

Could anyone please help me?

2 Answers 2

1

At magento 2.4 works this way:

    protected $quotesFactory;
    protected $storeManager;

    public function __construct(
        ...
        \Magento\Reports\Model\ResourceModel\Quote\CollectionFactory $quotesFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
        ...
    ) {
        ...
        $this->quotesFactory = $quotesFactory;
        $this->storeManager = $storeManager;
        ...
    }

At your function:

    $collection = $this->quotesFactory->create();
    $store_id = $this->storeManager->getStore()->getId();
    $collection->prepareForAbandonedReport([$store_id]);
    $rows = $collection->load();
    var_dump($rows->getData());exit;//or foreach($rows as $row){...}
0

Inject this below code in your construct :

protected $reportColl;
protected $storeManager;

public function __construct(
    \Magento\Reports\Model\ResourceModel\Quote\Collection $reportColl,
    \Magento\Store\Model\StoreManagerInterface $storeManager
){
    $this->reportColl = $reportColl;    
    $this->storeManager = $storeManager;
}

and then, use this below code in your function :

$collection = $this->reportColl->create();
$store_id = $this->storeManager->getStore()->getId();
$collection->prepareForAbandonedReport([$store_id]);
$rows = $collection->load();

Remove generated and clean cache.

3
  • 1
    Thanks. I will try it. Actually, my requirement is to collect the data and store it as a CSV. Could you please guide me?
    – Mohammad
    Commented Jan 6, 2020 at 13:16
  • I have tried this code but its not working. Could you please help?
    – Mohammad
    Commented Jan 8, 2020 at 9:17
  • I need to collect abandoned cart data. If you can share the sample code, that would be very helpful for me
    – Mohammad
    Commented Jan 8, 2020 at 9:18

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.