Skip to content

Commit 12a856a

Browse files
committed
MAGETWO-64282: Out of stock associated products to configurable are not full page cache cleaned #8009
- Clear cache for parent product when simple product becomes out of stock.
1 parent e8fda7c commit 12a856a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

app/code/Magento/CatalogInventory/Model/Indexer/Stock/CacheCleaner.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
use Magento\CatalogInventory\Api\StockConfigurationInterface;
1212
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\App\ObjectManager;
1314
use Magento\Framework\DB\Adapter\AdapterInterface;
1415
use Magento\Framework\Event\ManagerInterface;
16+
use Magento\Framework\EntityManager\MetadataPool;
1517
use Magento\Framework\Indexer\CacheContext;
1618
use Magento\CatalogInventory\Model\Stock;
1719
use Magento\Catalog\Model\Product;
@@ -46,22 +48,30 @@ class CacheCleaner
4648
*/
4749
private $connection;
4850

51+
/**
52+
* @var MetadataPool
53+
*/
54+
private $metadataPool;
55+
4956
/**
5057
* @param ResourceConnection $resource
5158
* @param StockConfigurationInterface $stockConfiguration
5259
* @param CacheContext $cacheContext
5360
* @param ManagerInterface $eventManager
61+
* @param MetadataPool|null $metadataPool
5462
*/
5563
public function __construct(
5664
ResourceConnection $resource,
5765
StockConfigurationInterface $stockConfiguration,
5866
CacheContext $cacheContext,
59-
ManagerInterface $eventManager
67+
ManagerInterface $eventManager,
68+
MetadataPool $metadataPool = null
6069
) {
6170
$this->resource = $resource;
6271
$this->stockConfiguration = $stockConfiguration;
6372
$this->cacheContext = $cacheContext;
6473
$this->eventManager = $eventManager;
74+
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
6575
}
6676

6777
/**
@@ -76,7 +86,7 @@ public function clean(array $productIds, callable $reindex)
7686
$productStatusesAfter = $this->getProductStockStatuses($productIds);
7787
$productIds = $this->getProductIdsForCacheClean($productStatusesBefore, $productStatusesAfter);
7888
if ($productIds) {
79-
$this->cacheContext->registerEntities(Product::CACHE_TAG, $productIds);
89+
$this->cacheContext->registerEntities(Product::CACHE_TAG, array_unique($productIds));
8090
$this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]);
8191
}
8292
}
@@ -87,11 +97,24 @@ public function clean(array $productIds, callable $reindex)
8797
*/
8898
private function getProductStockStatuses(array $productIds)
8999
{
100+
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
101+
->getLinkField();
90102
$select = $this->getConnection()->select()
91103
->from(
92-
$this->resource->getTableName('cataloginventory_stock_status'),
104+
['css' => $this->resource->getTableName('cataloginventory_stock_status')],
93105
['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)
95118
->where('stock_id = ?', Stock::DEFAULT_STOCK_ID)
96119
->where('website_id = ?', $this->stockConfiguration->getDefaultScopeId());
97120

@@ -125,6 +148,9 @@ private function getProductIdsForCacheClean(array $productStatusesBefore, array
125148
if ($statusBefore['stock_status'] !== $statusAfter['stock_status']
126149
|| ($stockThresholdQty && $statusAfter['qty'] <= $stockThresholdQty)) {
127150
$productIds[] = $productId;
151+
if (isset($statusAfter['parent_id'])) {
152+
$productIds[] = $statusAfter['parent_id'];
153+
}
128154
}
129155
}
130156

0 commit comments

Comments
 (0)