How can I get the abandoned cart collection on Magento 2? I want to collect it via controller.
Could anyone please help me?
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){...}
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.