1

I would like to remove items from cart for both logged in and guest shoppers.

3

You have to build a custom module which has cronjob.

https://devdocs.magento.com/guides/v2.2/config-guide/cron/custom-cron-tut.html

On the cronjob class Magento\SampleMinimal\Cron\Test you have to remove the quote item collection which is created 30min before.

Remove by QuoteITem

<?php


namespace Magento\SampleMinimal\Cron;

class Test {

    /**
     * @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);

                    }
                }


    }
}

Want to remove Quote then use

<?php


namespace Magento\SampleMinimal\Cron;

class Test {

    /**
     * @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\QuoteRepository $quoteRepository 
    ) {

        $this->quoteCollectionFactory = $quoteCollectionFactory;
        $this->quoteRepository = $quoteRepository;
    }
    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');
                $quoteCollection = $this->quoteCollectionFactory->create();
                /*
                $quoteCollection
                    ->addFieldToFilter('created_at', ['lteq' => $toDate])
                    ->addFieldToFilter('created_at', ['gteq' => $fromDate]);
                 * 
                 */
                $quoteCollection
                    ->addFieldToFilter('created_at', ['lteq' => $fromDate]);                
                //echo $quoteCollection->getSelect()->__toString();

                if($quoteCollection->getSize() >0){
                    foreach ($quoteCollection as $quote)
                    {
                        $quoteFullObject = $this->quoteRepository->get($quote->getId());
                        $quoteFullObject->delete();
                    }
                }


    }
}
| improve this answer | |
  • i have need code to get all cart item – Rahul Vaghela Sep 12 '18 at 8:23
  • you need quote item or quote? – Amit Bera Sep 12 '18 at 8:48

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.