9
9
10
10
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
11
11
use Magento \CatalogInventory \Model \ResourceModel \Stock \Item ;
12
- use Magento \CatalogInventory \Model \Stock ;
13
12
use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \PriceModifierInterface ;
14
13
use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \IndexTableStructure ;
15
14
use Magento \Framework \App \ResourceConnection ;
16
15
use Magento \Framework \App \ObjectManager ;
16
+ use Magento \Framework \DB \Query \Generator ;
17
17
18
18
/**
19
19
* Class for filter product price index.
@@ -40,22 +40,38 @@ class ProductPriceIndexFilter implements PriceModifierInterface
40
40
*/
41
41
private $ connectionName ;
42
42
43
+ /**
44
+ * @var Generator
45
+ */
46
+ private $ batchQueryGenerator ;
47
+
48
+ /**
49
+ * @var int
50
+ */
51
+ private $ batchSize ;
52
+
43
53
/**
44
54
* @param StockConfigurationInterface $stockConfiguration
45
55
* @param Item $stockItem
46
56
* @param ResourceConnection $resourceConnection
47
57
* @param string $connectionName
58
+ * @param Generator $batchQueryGenerator
59
+ * @param int $batchSize
48
60
*/
49
61
public function __construct (
50
62
StockConfigurationInterface $ stockConfiguration ,
51
63
Item $ stockItem ,
52
64
ResourceConnection $ resourceConnection = null ,
53
- $ connectionName = 'indexer '
65
+ $ connectionName = 'indexer ' ,
66
+ Generator $ batchQueryGenerator = null ,
67
+ $ batchSize = 100
54
68
) {
55
69
$ this ->stockConfiguration = $ stockConfiguration ;
56
70
$ this ->stockItem = $ stockItem ;
57
71
$ this ->resourceConnection = $ resourceConnection ?: ObjectManager::getInstance ()->get (ResourceConnection::class);
58
72
$ this ->connectionName = $ connectionName ;
73
+ $ this ->batchQueryGenerator = $ batchQueryGenerator ?: ObjectManager::getInstance ()->get (Generator::class);
74
+ $ this ->batchSize = $ batchSize ;
59
75
}
60
76
61
77
/**
@@ -76,32 +92,37 @@ public function modifyPrice(IndexTableStructure $priceTable, array $entityIds =
76
92
77
93
$ connection = $ this ->resourceConnection ->getConnection ($ this ->connectionName );
78
94
$ select = $ connection ->select ();
95
+
79
96
$ select ->from (
80
- ['price_index ' => $ priceTable ->getTableName ()],
81
- []
82
- );
83
- $ select ->joinInner (
84
97
['stock_item ' => $ this ->stockItem ->getMainTable ()],
85
- 'stock_item.product_id = price_index. ' . $ priceTable ->getEntityField ()
86
- . ' AND stock_item.stock_id = ' . Stock::DEFAULT_STOCK_ID ,
87
- []
98
+ ['stock_item.product_id ' , 'MAX(stock_item.is_in_stock) as max_is_in_stock ' ]
88
99
);
100
+
89
101
if ($ this ->stockConfiguration ->getManageStock ()) {
90
- $ stockStatus = $ connection ->getCheckSql (
91
- 'use_config_manage_stock = 0 AND manage_stock = 0 ' ,
92
- Stock::STOCK_IN_STOCK ,
93
- 'is_in_stock '
94
- );
102
+ $ select ->where ('stock_item.use_config_manage_stock = 1 OR stock_item.manage_stock = 1 ' );
95
103
} else {
96
- $ stockStatus = $ connection ->getCheckSql (
97
- 'use_config_manage_stock = 0 AND manage_stock = 1 ' ,
98
- 'is_in_stock ' ,
99
- Stock::STOCK_IN_STOCK
100
- );
104
+ $ select ->where ('stock_item.use_config_manage_stock = 0 AND stock_item.manage_stock = 1 ' );
101
105
}
102
- $ select ->where ($ stockStatus . ' = ? ' , Stock::STOCK_OUT_OF_STOCK );
103
106
104
- $ query = $ select ->deleteFromSelect ('price_index ' );
105
- $ connection ->query ($ query );
107
+ $ select ->group ('stock_item.product_id ' );
108
+ $ select ->having ('max_is_in_stock = 0 ' );
109
+
110
+ $ batchSelectIterator = $ this ->batchQueryGenerator ->generate (
111
+ 'product_id ' ,
112
+ $ select ,
113
+ $ this ->batchSize ,
114
+ \Magento \Framework \DB \Query \BatchIteratorInterface::UNIQUE_FIELD_ITERATOR
115
+ );
116
+
117
+ foreach ($ batchSelectIterator as $ select ) {
118
+ $ productIds = null ;
119
+ foreach ($ connection ->query ($ select )->fetchAll () as $ row ) {
120
+ $ productIds [] = $ row ['product_id ' ];
121
+ }
122
+ if ($ productIds !== null ) {
123
+ $ where = [$ priceTable ->getEntityField () .' IN (?) ' => $ productIds ];
124
+ $ connection ->delete ($ priceTable ->getTableName (), $ where );
125
+ }
126
+ }
106
127
}
107
128
}
0 commit comments