99
1010use Magento \CatalogInventory \Api \StockConfigurationInterface ;
1111use Magento \CatalogInventory \Model \ResourceModel \Stock \Item ;
12- use Magento \CatalogInventory \Model \Stock ;
1312use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \PriceModifierInterface ;
1413use Magento \Catalog \Model \ResourceModel \Product \Indexer \Price \IndexTableStructure ;
1514use Magento \Framework \App \ResourceConnection ;
1615use Magento \Framework \App \ObjectManager ;
16+ use Magento \Framework \DB \Query \Generator ;
1717
1818/**
1919 * Class for filter product price index.
@@ -40,22 +40,38 @@ class ProductPriceIndexFilter implements PriceModifierInterface
4040 */
4141 private $ connectionName ;
4242
43+ /**
44+ * @var Generator
45+ */
46+ private $ batchQueryGenerator ;
47+
48+ /**
49+ * @var int
50+ */
51+ private $ batchSize ;
52+
4353 /**
4454 * @param StockConfigurationInterface $stockConfiguration
4555 * @param Item $stockItem
4656 * @param ResourceConnection $resourceConnection
4757 * @param string $connectionName
58+ * @param Generator $batchQueryGenerator
59+ * @param int $batchSize
4860 */
4961 public function __construct (
5062 StockConfigurationInterface $ stockConfiguration ,
5163 Item $ stockItem ,
5264 ResourceConnection $ resourceConnection = null ,
53- $ connectionName = 'indexer '
65+ $ connectionName = 'indexer ' ,
66+ Generator $ batchQueryGenerator = null ,
67+ $ batchSize = 100
5468 ) {
5569 $ this ->stockConfiguration = $ stockConfiguration ;
5670 $ this ->stockItem = $ stockItem ;
5771 $ this ->resourceConnection = $ resourceConnection ?: ObjectManager::getInstance ()->get (ResourceConnection::class);
5872 $ this ->connectionName = $ connectionName ;
73+ $ this ->batchQueryGenerator = $ batchQueryGenerator ?: ObjectManager::getInstance ()->get (Generator::class);
74+ $ this ->batchSize = $ batchSize ;
5975 }
6076
6177 /**
@@ -76,32 +92,37 @@ public function modifyPrice(IndexTableStructure $priceTable, array $entityIds =
7692
7793 $ connection = $ this ->resourceConnection ->getConnection ($ this ->connectionName );
7894 $ select = $ connection ->select ();
95+
7996 $ select ->from (
80- ['price_index ' => $ priceTable ->getTableName ()],
81- []
82- );
83- $ select ->joinInner (
8497 ['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 ' ]
8899 );
100+
89101 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 ' );
95103 } 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 ' );
101105 }
102- $ select ->where ($ stockStatus . ' = ? ' , Stock::STOCK_OUT_OF_STOCK );
103106
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+ }
106127 }
107128}
0 commit comments