Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean expired quotes - Fix out of memory on huge quotes list #27260

Merged
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();
}
}
}
}