Skip to content

Commit f04154e

Browse files
Add child identities of bundle and configurable products on frontend instead of parent identities
1 parent 9845b2b commit f04154e

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Bundle\Model\Plugin\Frontend;
7+
8+
use Magento\Bundle\Model\Product\Type;
9+
use Magento\Catalog\Model\Product as CatalogProduct;
10+
11+
class Product
12+
{
13+
/**
14+
* @var Type
15+
*/
16+
private $type;
17+
18+
/**
19+
* @param Type $type
20+
*/
21+
public function __construct(Type $type)
22+
{
23+
$this->type = $type;
24+
}
25+
26+
/**
27+
* Add child identities to product identities
28+
*
29+
* @param CatalogProduct $product
30+
* @param array $identities
31+
* @return string[]
32+
*/
33+
public function afterGetIdentities(
34+
CatalogProduct $product,
35+
array $identities
36+
) {
37+
foreach ($this->type->getChildrenIds($product->getEntityId()) as $optionId => $childIds) {
38+
foreach ($childIds as $childId) {
39+
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId;
40+
}
41+
}
42+
43+
return array_unique($identities);
44+
}
45+
}

app/code/Magento/Bundle/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\Catalog\Model\Product">
17+
<plugin name="bundle" type="Magento\Bundle\Model\Plugin\Frontend\Product" sortOrder="100" />
18+
</type>
1619
</config>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ConfigurableProduct\Model\Plugin\Frontend;
9+
10+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product;
13+
14+
/**
15+
* Extender of product identities for child of configurable products
16+
*/
17+
class ProductIdentitiesExtender
18+
{
19+
/**
20+
* @var Configurable
21+
*/
22+
private $configurableType;
23+
24+
/**
25+
* @var ProductRepositoryInterface
26+
*/
27+
private $productRepository;
28+
29+
/**
30+
* @param Configurable $configurableType
31+
* @param ProductRepositoryInterface $productRepository
32+
*/
33+
public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository)
34+
{
35+
$this->configurableType = $configurableType;
36+
$this->productRepository = $productRepository;
37+
}
38+
39+
/**
40+
* Add child identities to product identities
41+
*
42+
* @param Product $subject
43+
* @param array $identities
44+
* @return array
45+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
46+
*/
47+
public function afterGetIdentities(Product $subject, array $identities): array
48+
{
49+
foreach ($this->configurableType->getChildrenIds($subject->getId()) as $key => $childIds) {
50+
foreach ($childIds as $childId) {
51+
$identities[] = Product::CACHE_TAG . '_' . $childId;
52+
}
53+
}
54+
55+
return array_unique($identities);
56+
}
57+
}

app/code/Magento/ConfigurableProduct/etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<type name="Magento\ConfigurableProduct\Model\ResourceModel\Attribute\OptionSelectBuilderInterface">
1111
<plugin name="Magento_ConfigurableProduct_Plugin_Model_ResourceModel_Attribute_InStockOptionSelectBuilder" type="Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Attribute\InStockOptionSelectBuilder"/>
1212
</type>
13+
<type name="Magento\Catalog\Model\Product">
14+
<plugin name="product_identities_extender" type="Magento\ConfigurableProduct\Model\Plugin\Frontend\ProductIdentitiesExtender" />
15+
</type>
1316
</config>

0 commit comments

Comments
 (0)