We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Step 1: create a “Registration.php” file inside our extension at the following path.

    Path: appcodeMagemonkeysRemovecart

    <?php
    MagentoFrameworkComponentComponentRegistrar::register(
        MagentoFrameworkComponentComponentRegistrar::MODULE,
        'Magemonkeys_Removecart',
        __DIR__
    );

    Step 2: After that,  create a “Module.xml” file inside extension etc folder. appcodeMagemonkeysRemovecartetc

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Magemonkeys_Removecart" setup_version="1.0.0" schema_version="1.0.0">
        </module>
    </config>

    Step 3: After that, create “crontab.xml” file inside extension etc folder. appcodeMagemonkeysRemovecartetc

    <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="removecart" instance="MagemonkeysRemovecartCronRemovecart" method="getItemData">
                <schedule>* * * * *</schedule>
            </job>
        </group>
    </config>

    Step 4: Lastly, Create the “Removecart.php” file inside the Cron folder of extension. appcodeMagemonkeysRemovecartCron

    <?php
    namespace MagemonkeysRemovecartCron;
    
    class Removecart {
    
        /**
         * @var MagentoQuoteModelQuoteRepository
         */
        protected $quoteRepository;
    
        /**
         * @var MagentoQuoteModelResourceModelQuoteCollectionFactory
         */
        protected $quoteCollectionFactory;
    
        public function __construct(
            MagentoQuoteModelResourceModelQuoteCollectionFactory $quoteCollectionFactory,
            MagentoQuoteModelQuoteRepository $quoteRepository
        ) {
    
            $this->quoteCollectionFactory = $quoteCollectionFactory;
            $this->quoteRepository = $quoteRepository;
        }
        public function getItemData()
        {
            $fromTime = new DateTime('now', new DateTimezone('UTC'));
            $fromTime->sub(DateInterval::createFromDateString('30 minutes'));
    
            $fromDate = $fromTime->format('Y-m-d H:i:s');
            $quoteCollection = $this->quoteCollectionFactory->create();
    
            $quoteCollection
                ->addFieldToFilter('created_at', ['lteq' => $fromDate]);
    
            if($quoteCollection->getSize() >0){
                foreach ($quoteCollection as $quote)
                {
                    $quoteFullObject = $this->quoteRepository->get($quote->getId());
                    $quoteFullObject->delete();
                }
            }
    
    
        }
    }

    Run below command in your command prompt

    php bin/magento cron:install

    php bin/magento cron:run

    Every 30 minutes, the cron is executed. If there is an item available in the cart, it will be then removed, automatically!

     

    field_5bfb909c5ccae

      Get a Free Quote