1111use Magento \CatalogInventory \Api \StockConfigurationInterface ;
1212use Magento \CatalogInventory \Model \Indexer \ProductPriceIndexFilter ;
1313use Magento \Framework \App \ResourceConnection ;
14+ use Magento \InventoryApi \Api \Data \StockInterface ;
15+ use Magento \InventoryCatalogApi \Api \DefaultStockProviderInterface ;
1416use Magento \InventoryIndexer \Model \StockIndexTableNameResolverInterface ;
1517use Magento \InventorySalesApi \Model \StockByWebsiteIdResolverInterface ;
1618
@@ -39,22 +41,30 @@ class ModifySelectInProductPriceIndexFilter
3941 */
4042 private $ stockByWebsiteIdResolver ;
4143
44+ /**
45+ * @var DefaultStockProviderInterface
46+ */
47+ private $ defaultStockProvider ;
48+
4249 /**
4350 * @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver
4451 * @param StockConfigurationInterface $stockConfiguration
4552 * @param ResourceConnection $resourceConnection
4653 * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
54+ * @param DefaultStockProviderInterface $defaultStockProvider
4755 */
4856 public function __construct (
4957 StockIndexTableNameResolverInterface $ stockIndexTableNameResolver ,
5058 StockConfigurationInterface $ stockConfiguration ,
5159 ResourceConnection $ resourceConnection ,
52- StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver
60+ StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver ,
61+ DefaultStockProviderInterface $ defaultStockProvider
5362 ) {
5463 $ this ->stockIndexTableNameResolver = $ stockIndexTableNameResolver ;
5564 $ this ->stockConfiguration = $ stockConfiguration ;
5665 $ this ->resourceConnection = $ resourceConnection ;
5766 $ this ->stockByWebsiteIdResolver = $ stockByWebsiteIdResolver ;
67+ $ this ->defaultStockProvider = $ defaultStockProvider ;
5868 }
5969
6070 /**
@@ -84,7 +94,8 @@ public function aroundModifyPrice(
8494 $ select ->from (['price_index ' => $ priceTable ->getTableName ()], []);
8595 $ priceEntityField = $ priceTable ->getEntityField ();
8696
87- if ($ this ->resourceConnection ->getConnection ()->isTableExists ($ stockTable )) {
97+ if (!$ this ->isDefaultStock ($ stock )
98+ && $ this ->resourceConnection ->getConnection ()->isTableExists ($ stockTable )) {
8899 $ select ->joinInner (
89100 ['product_entity ' => $ this ->resourceConnection ->getTableName ('catalog_product_entity ' )],
90101 "product_entity.entity_id = price_index. {$ priceEntityField }" ,
@@ -95,6 +106,17 @@ public function aroundModifyPrice(
95106 []
96107 );
97108 $ select ->where ('inventory_stock.is_salable = 0 OR inventory_stock.is_salable IS NULL ' );
109+ } else {
110+ $ legacyStockTableName = $ this ->resourceConnection ->getTableName ('cataloginventory_stock_status ' );
111+ $ select ->joinLeft (
112+ ['stock_status ' => $ legacyStockTableName ],
113+ sprintf (
114+ 'stock_status.product_id = price_index.%s ' ,
115+ $ priceEntityField
116+ ),
117+ []
118+ );
119+ $ select ->where ('stock_status.stock_status = 0 OR stock_status.stock_status IS NULL ' );
98120 }
99121
100122 $ select ->where ('price_index.website_id = ? ' , $ websiteId );
@@ -126,4 +148,15 @@ private function getWebsiteIdsFromProducts(array $entityIds): array
126148
127149 return $ result ;
128150 }
151+
152+ /**
153+ * Checks if inventory stock is DB view
154+ *
155+ * @param StockInterface $stock
156+ * @return bool
157+ */
158+ private function isDefaultStock (StockInterface $ stock ): bool
159+ {
160+ return (int )$ stock ->getStockId () === $ this ->defaultStockProvider ->getId ();
161+ }
129162}
0 commit comments