10
10
11
11
use Magento \CatalogInventory \Api \StockConfigurationInterface ;
12
12
use Magento \Framework \App \ResourceConnection ;
13
+ use Magento \Framework \App \ObjectManager ;
13
14
use Magento \Framework \DB \Adapter \AdapterInterface ;
14
15
use Magento \Framework \Event \ManagerInterface ;
16
+ use Magento \Framework \EntityManager \MetadataPool ;
15
17
use Magento \Framework \Indexer \CacheContext ;
16
18
use Magento \CatalogInventory \Model \Stock ;
17
19
use Magento \Catalog \Model \Product ;
@@ -46,22 +48,30 @@ class CacheCleaner
46
48
*/
47
49
private $ connection ;
48
50
51
+ /**
52
+ * @var MetadataPool
53
+ */
54
+ private $ metadataPool ;
55
+
49
56
/**
50
57
* @param ResourceConnection $resource
51
58
* @param StockConfigurationInterface $stockConfiguration
52
59
* @param CacheContext $cacheContext
53
60
* @param ManagerInterface $eventManager
61
+ * @param MetadataPool|null $metadataPool
54
62
*/
55
63
public function __construct (
56
64
ResourceConnection $ resource ,
57
65
StockConfigurationInterface $ stockConfiguration ,
58
66
CacheContext $ cacheContext ,
59
- ManagerInterface $ eventManager
67
+ ManagerInterface $ eventManager ,
68
+ MetadataPool $ metadataPool = null
60
69
) {
61
70
$ this ->resource = $ resource ;
62
71
$ this ->stockConfiguration = $ stockConfiguration ;
63
72
$ this ->cacheContext = $ cacheContext ;
64
73
$ this ->eventManager = $ eventManager ;
74
+ $ this ->metadataPool = $ metadataPool ?: ObjectManager::getInstance ()->get (MetadataPool::class);
65
75
}
66
76
67
77
/**
@@ -76,7 +86,7 @@ public function clean(array $productIds, callable $reindex)
76
86
$ productStatusesAfter = $ this ->getProductStockStatuses ($ productIds );
77
87
$ productIds = $ this ->getProductIdsForCacheClean ($ productStatusesBefore , $ productStatusesAfter );
78
88
if ($ productIds ) {
79
- $ this ->cacheContext ->registerEntities (Product::CACHE_TAG , $ productIds );
89
+ $ this ->cacheContext ->registerEntities (Product::CACHE_TAG , array_unique ( $ productIds) );
80
90
$ this ->eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContext ]);
81
91
}
82
92
}
@@ -87,11 +97,24 @@ public function clean(array $productIds, callable $reindex)
87
97
*/
88
98
private function getProductStockStatuses (array $ productIds )
89
99
{
100
+ $ linkField = $ this ->metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
101
+ ->getLinkField ();
90
102
$ select = $ this ->getConnection ()->select ()
91
103
->from (
92
- $ this ->resource ->getTableName ('cataloginventory_stock_status ' ),
104
+ [ ' css ' => $ this ->resource ->getTableName ('cataloginventory_stock_status ' )] ,
93
105
['product_id ' , 'stock_status ' , 'qty ' ]
94
- )->where ('product_id IN (?) ' , $ productIds )
106
+ )
107
+ ->joinLeft (
108
+ ['cpr ' => $ this ->resource ->getTableName ('catalog_product_relation ' )],
109
+ 'css.product_id = cpr.child_id ' ,
110
+ []
111
+ )
112
+ ->joinLeft (
113
+ ['cpe ' => $ this ->resource ->getTableName ('catalog_product_entity ' )],
114
+ 'cpr.parent_id = cpe. ' . $ linkField ,
115
+ ['parent_id ' => 'cpe.entity_id ' ]
116
+ )
117
+ ->where ('product_id IN (?) ' , $ productIds )
95
118
->where ('stock_id = ? ' , Stock::DEFAULT_STOCK_ID )
96
119
->where ('website_id = ? ' , $ this ->stockConfiguration ->getDefaultScopeId ());
97
120
@@ -125,6 +148,9 @@ private function getProductIdsForCacheClean(array $productStatusesBefore, array
125
148
if ($ statusBefore ['stock_status ' ] !== $ statusAfter ['stock_status ' ]
126
149
|| ($ stockThresholdQty && $ statusAfter ['qty ' ] <= $ stockThresholdQty )) {
127
150
$ productIds [] = $ productId ;
151
+ if (isset ($ statusAfter ['parent_id ' ])) {
152
+ $ productIds [] = $ statusAfter ['parent_id ' ];
153
+ }
128
154
}
129
155
}
130
156
0 commit comments