Skip to content

Commit 28f00aa

Browse files
author
Oleksandr Dubovyk
committed
MC-20212: [On premise] Data loss due to core cron job called sales_clean_quotes
- fixed - modified tests
1 parent d6ef3ef commit 28f00aa

File tree

3 files changed

+116
-137
lines changed

3 files changed

+116
-137
lines changed

app/code/Magento/Sales/Cron/CleanExpiredQuotes.php

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,35 @@
55
*/
66
namespace Magento\Sales\Cron;
77

8-
use Magento\Store\Model\StoresConfig;
8+
use Magento\Quote\Model\ResourceModel\Quote\Collection;
9+
use Magento\Sales\Helper\ExpiredQuotesCollection;
10+
use Magento\Store\Model\StoreManagerInterface;
911

1012
/**
1113
* Class CleanExpiredQuotes
1214
*/
1315
class CleanExpiredQuotes
1416
{
15-
const LIFETIME = 86400;
16-
17-
/**
18-
* @var StoresConfig
19-
*/
20-
protected $storesConfig;
21-
2217
/**
23-
* @var \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory
18+
* @var ExpiredQuotesCollection
2419
*/
25-
protected $quoteCollectionFactory;
20+
private $expiredQuotesCollection;
2621

2722
/**
28-
* @var array
23+
* @var StoreManagerInterface
2924
*/
30-
protected $expireQuotesFilterFields = [];
25+
private $storeManager;
3126

3227
/**
33-
* @param StoresConfig $storesConfig
34-
* @param \Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
28+
* @param StoreManagerInterface $storeManager
29+
* @param ExpiredQuotesCollection $expiredQuotesCollection
3530
*/
3631
public function __construct(
37-
StoresConfig $storesConfig,
38-
\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory $collectionFactory
32+
StoreManagerInterface $storeManager,
33+
ExpiredQuotesCollection $expiredQuotesCollection
3934
) {
40-
$this->storesConfig = $storesConfig;
41-
$this->quoteCollectionFactory = $collectionFactory;
35+
$this->storeManager = $storeManager;
36+
$this->expiredQuotesCollection = $expiredQuotesCollection;
4237
}
4338

4439
/**
@@ -48,42 +43,11 @@ public function __construct(
4843
*/
4944
public function execute()
5045
{
51-
$lifetimes = $this->storesConfig->getStoresConfigByPath('checkout/cart/delete_quote_after');
52-
foreach ($lifetimes as $storeId => $lifetime) {
53-
$lifetime *= self::LIFETIME;
54-
55-
/** @var $quotes \Magento\Quote\Model\ResourceModel\Quote\Collection */
56-
$quotes = $this->quoteCollectionFactory->create();
57-
58-
$quotes->addFieldToFilter('store_id', $storeId);
59-
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);
60-
61-
foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
62-
$quotes->addFieldToFilter($field, $condition);
63-
}
64-
46+
$stores = $this->storeManager->getStores(true);
47+
foreach ($stores as $store) {
48+
/** @var $quotes Collection */
49+
$quotes = $this->expiredQuotesCollection->getExpiredQuotes($store);
6550
$quotes->walk('delete');
6651
}
6752
}
68-
69-
/**
70-
* Retrieve expire quotes additional fields to filter
71-
*
72-
* @return array
73-
*/
74-
protected function getExpireQuotesAdditionalFilterFields()
75-
{
76-
return $this->expireQuotesFilterFields;
77-
}
78-
79-
/**
80-
* Set expire quotes additional fields to filter
81-
*
82-
* @param array $fields
83-
* @return void
84-
*/
85-
public function setExpireQuotesAdditionalFilterFields(array $fields)
86-
{
87-
$this->expireQuotesFilterFields = $fields;
88-
}
8953
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Helper;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Quote\Model\ResourceModel\Quote\Collection;
10+
use Magento\Quote\Model\ResourceModel\Quote\CollectionFactory;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Store\Model\ScopeInterface;
13+
14+
/**
15+
* Class ExpiredQuotesCollection
16+
*/
17+
class ExpiredQuotesCollection
18+
{
19+
const SECONDS_IN_DAY = 86400;
20+
const QUOTE_LIFETIME = 'checkout/cart/delete_quote_after';
21+
22+
/**
23+
* @var CollectionFactory
24+
*/
25+
private $quoteCollectionFactory;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $expireQuotesFilterFields = [];
31+
32+
/**
33+
* @var ScopeConfigInterface
34+
*/
35+
private $config;
36+
37+
/**
38+
* @param ScopeConfigInterface $config
39+
* @param CollectionFactory $collectionFactory
40+
*/
41+
public function __construct(
42+
ScopeConfigInterface $config,
43+
CollectionFactory $collectionFactory
44+
) {
45+
$this->config = $config;
46+
$this->quoteCollectionFactory = $collectionFactory;
47+
}
48+
49+
/**
50+
* Gets expired quotes
51+
*
52+
* Quote is considered expired if the latest update date
53+
* of the quote is greater than lifetime threshold
54+
*
55+
* @param StoreInterface $store
56+
* @return Collection
57+
*/
58+
public function getExpiredQuotes(StoreInterface $store)
59+
{
60+
$lifetime = $this->config->getValue(
61+
self::QUOTE_LIFETIME,
62+
ScopeInterface::SCOPE_STORE,
63+
$store->getCode()
64+
);
65+
$lifetime *= self::SECONDS_IN_DAY;
66+
67+
/** @var $quotes Collection */
68+
$quotes = $this->quoteCollectionFactory->create();
69+
$quotes->addFieldToFilter('store_id', $store->getId());
70+
$quotes->addFieldToFilter('updated_at', ['to' => date("Y-m-d", time() - $lifetime)]);
71+
72+
foreach ($this->getExpireQuotesAdditionalFilterFields() as $field => $condition) {
73+
$quotes->addFieldToFilter($field, $condition);
74+
}
75+
76+
return $quotes;
77+
}
78+
79+
/**
80+
* Retrieve expire quotes additional fields to filter
81+
*
82+
* @return array
83+
*/
84+
private function getExpireQuotesAdditionalFilterFields()
85+
{
86+
return $this->expireQuotesFilterFields;
87+
}
88+
89+
/**
90+
* Set expire quotes additional fields to filter
91+
*
92+
* @param array $fields
93+
* @return void
94+
*/
95+
public function setExpireQuotesAdditionalFilterFields(array $fields)
96+
{
97+
$this->expireQuotesFilterFields = $fields;
98+
}
99+
}

app/code/Magento/Sales/Test/Unit/Cron/CleanExpiredQuotesTest.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)