0

Guest / Logged-in user:

  • Auto removes items from the cart after 24 hours.
  • Canceling and returning from the payment page auto-remove cart after 24 hours.
0

You can create your custom cronjob, please see my answer below.

1. crontab.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job name="remove_cart_items" instance="Your\Module\Cron\DeleteQuote" method="execute">
            <schedule>* */24 * * *</schedule>
        </job>
    </group>
</config>  

2. DeleteQuote.php

<?php

namespace Your\Module\Cron;

use \Psr\Log\LoggerInterface;

class DeleteQuote {

    protected $logger;

    protected $_quoteFactory;

    public function __construct(
        LoggerInterface $logger,
        \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteFactory,
    ) {
        $this->logger = $logger;
        $this->_quoteFactory = $quoteFactory;
    }

    public function execute() {
        try {
            $this->logger->info('Cron run of quote delete.');
            $collection = $this->_quoteFactory->create();
            $count = 0;

            foreach ($collection as $value) {
                if(count($value->getItemsCollection()->getData())){
                    foreach ($value->getItemsCollection() as $item) {
                        /*log which quote is deleted*/
                        $this->logger->info('Cron delete of stock delete '. $item->getId());
                        $item->delete();
                        $count++;
                    }
                }   
            }
            $this->logger->info('Total Deleted quotes'. $count);

        } catch (\Exception $e) {
            $this->logger->info('Cron Error '. $e->getMessage());
        }
    }
}
| improve this answer | |
0

You need to create a Custom module & Set Cron which will run after a certain interval of time.

In that you can use below code

<?php
namespace Company\Custom\Cron;

class Custom {

    /**
     * @var \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory
     */
    protected $itemCollectionFactory;

    /**
     * @var \Magento\Quote\Model\QuoteRepository
     */
    protected $quoteRepository;

    /**
     * @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
     */
    protected $quoteCollectionFactory;

    public function __construct(
        \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $quoteCollectionFactory,
        \Magento\Quote\Model\ResourceModel\Quote\Item\CollectionFactory $itemCollectionFactory,  
        \Magento\Quote\Model\QuoteRepository $quoteRepository 
    ) {

        $this->quoteCollectionFactory = $quoteCollectionFactory;
        $this->quoteRepository = $quoteRepository;
        $this->itemCollectionFactory = $itemCollectionFactory;
    }
    public function getItemData()
    {   
        $fromTime = new \DateTime('now', new \DateTimezone('UTC'));
        $toTime = clone $fromTime;
        $fromTime->sub(\DateInterval::createFromDateString('30 minutes'));
        echo  $fromDate = $fromTime->format('Y-m-d H:i:s');
        echo $toDate = $toTime->format('Y-m-d H:i:s');


        $itemColelction =$this->itemCollectionFactory->create()
              ->addFieldToSelect('created_at')
               ->addFieldToSelect('item_id')
               ->addFieldToSelect('quote_id')
               ->addFieldToFilter('created_at', ['lteq' => $fromDate]);     

        if($quoteCollection->getSize() >0){
            foreach ($itemColelction as $item)
            {
                $quote = $this->quoteRepository->get($item->getQuoteId());
                $quote->deleteItem($item);

            }
        }
    }
}
| improve this answer | |
  • Hi how to use the above code ? – bhas kar Mar 18 at 12:23
  • Create a custom module to run cron & from there call this function – Arjun Mar 18 at 12:38
  • set the value as ini_set('session.gc_maxlifetime', 60*60*24); otherwise set value as 86400 in your php.ini /etc/php/7.1/apache2/php.ini – bhas kar Mar 20 at 9:39
0

I done this, in a simple method.

set the value as

ini_set('session.cookie_lifetime', 60 * 60 * 24); ini_set('session.gc_maxlifetime', 60 * 60 * 24);

otherwise set value as 86400 in your php.ini

/etc/php/7.1/apache2/php.ini enter image description here

enter image description here

| improve this answer | |

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.