0

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

Could anyone please help me?

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
  • 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 Jan 6 '20 at 13:16
  • I have tried this code but its not working. Could you please help? – Mohammad Jan 8 '20 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 Jan 8 '20 at 9:18
0

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){...}

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.