Skip to content

Commit

Permalink
Clean expired quotes - Fix out of memory on huge quotes list
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor-sviziev committed Mar 12, 2020
1 parent 047f5b9 commit 1031b3b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/code/Magento/Sales/Cron/CleanExpiredQuotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
namespace Magento\Sales\Cron;

use Magento\Quote\Model\ResourceModel\Quote\Collection;
use Magento\Quote\Model\ResourceModel\Quote\Collection as QuoteCollection;
use Magento\Sales\Model\ResourceModel\Collection\ExpiredQuotesCollection;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class CleanExpiredQuotes
* Cron job for cleaning expired Quotes
*/
class CleanExpiredQuotes
{
Expand Down Expand Up @@ -45,9 +45,20 @@ public function execute()
{
$stores = $this->storeManager->getStores(true);
foreach ($stores as $store) {
/** @var $quotes Collection */
$quotes = $this->expiredQuotesCollection->getExpiredQuotes($store);
$quotes->walk('delete');
/** @var $quoteCollection QuoteCollection */
$quoteCollection = $this->expiredQuotesCollection->getExpiredQuotes($store);
$quoteCollection->setPageSize(50);

// Last page returns 1 even when we don't have any results
$lastPage = $quoteCollection->getSize() ? $quoteCollection->getLastPageNumber() : 0;

for ($currentPage = 1; $currentPage <= $lastPage; $currentPage++) {
$quoteCollection->setCurPage($currentPage);

$quoteCollection->walk('delete');

$quoteCollection->clear();
}
}
}
}

0 comments on commit 1031b3b

Please sign in to comment.