Skip to content

Commit 83d79dd

Browse files
committed
MAGETWO-83204: "Hide from Product Page" option does not work for child of configurable product
1 parent e3e250f commit 83d79dd

File tree

9 files changed

+63
-90
lines changed

9 files changed

+63
-90
lines changed

app/code/Magento/Catalog/Model/Product.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,12 +1058,7 @@ protected function _afterLoad()
10581058
*/
10591059
public function cleanCache()
10601060
{
1061-
if ($this->getId()) {
1062-
$this->_cacheManager->clean(
1063-
self::CACHE_TAG . '_' . $this->getId()
1064-
);
1065-
}
1066-
return $this;
1061+
return parent::cleanModelCache();
10671062
}
10681063

10691064
/**

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,21 +1391,6 @@ public function testGetOptionByIdForProductWithoutOptions()
13911391
$this->assertNull($this->model->getOptionById(100));
13921392
}
13931393

1394-
public function testCleanCache()
1395-
{
1396-
//Without an ID cleanCache won't clean anything because the entity is
1397-
//not identified and it will be called later exactly once.
1398-
$this->model->setId(null);
1399-
$this->cacheManagerMock
1400-
->expects($this->once())
1401-
->method('clean');
1402-
$this->model->cleanCache();
1403-
1404-
//Now that ID is set clean will be called.
1405-
$this->model->setId(1);
1406-
$this->model->cleanCache();
1407-
}
1408-
14091394
public function testGetCacheTags()
14101395
{
14111396
//If entity is identified getCacheTags has to return the same values

app/code/Magento/ConfigurableProduct/Model/Plugin/CleanCache.php

Lines changed: 0 additions & 49 deletions
This file was deleted.
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+
namespace Magento\ConfigurableProduct\Model\Plugin;
7+
8+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product;
11+
12+
/**
13+
* Extender of product identities for child of configurable products
14+
*/
15+
class ProductIdentitiesExtender
16+
{
17+
/**
18+
* @var Configurable
19+
*/
20+
private $configurableType;
21+
22+
/**
23+
* @var ProductRepositoryInterface
24+
*/
25+
private $productRepository;
26+
27+
/**
28+
* @param Configurable $configurableType
29+
* @param ProductRepositoryInterface $productRepository
30+
*/
31+
public function __construct(Configurable $configurableType, ProductRepositoryInterface $productRepository)
32+
{
33+
$this->configurableType = $configurableType;
34+
$this->productRepository = $productRepository;
35+
}
36+
37+
/**
38+
* Add parent identities to product identities
39+
*
40+
* @param Product $subject
41+
* @param \Closure $proceed
42+
* @return array
43+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
44+
*/
45+
public function aroundGetIdentities(Product $subject, \Closure $proceed)
46+
{
47+
$identities = $proceed();
48+
49+
foreach ($this->configurableType->getParentIdsByChild($subject->getId()) as $parentId) {
50+
$parentProduct = $this->productRepository->getById($parentId);
51+
$identities = array_merge($identities, $parentProduct->getIdentities());
52+
}
53+
$identities = array_unique($identities);
54+
55+
return $identities;
56+
}
57+
}

app/code/Magento/ConfigurableProduct/Model/Product/Cache/Tag/Configurable.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public function getTags($object)
4343

4444
$result = $object->getIdentities();
4545

46-
foreach ($this->catalogProductTypeConfigurable->getParentIdsByChild($object->getId()) as $parentId) {
47-
$result[] = \Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId;
48-
}
4946
return $result;
5047
}
5148
}

app/code/Magento/ConfigurableProduct/Model/ResourceModel/Product/Type/Configurable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ public function getChildrenIds($parentId, $required = true)
169169
*/
170170
public function getParentIdsByChild($childId)
171171
{
172-
$parentIds = [];
173172
$select = $this->getConnection()
174173
->select()
175174
->from(['l' => $this->getMainTable()], [])
@@ -178,10 +177,7 @@ public function getParentIdsByChild($childId)
178177
'e.' . $this->optionProvider->getProductEntityLinkField() . ' = l.parent_id',
179178
['e.entity_id']
180179
)->where('l.product_id IN(?)', $childId);
181-
182-
foreach ($this->getConnection()->fetchAll($select) as $row) {
183-
$parentIds[] = $row['entity_id'];
184-
}
180+
$parentIds = $this->getConnection()->fetchCol($select);
185181

186182
return $parentIds;
187183
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Cache/Tag/ConfigurableTest.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ public function testGetTagsWithVariation()
5656
->method('getIdentities')
5757
->willReturn($identities);
5858

59-
$parentId = 4;
60-
$this->typeResource->expects($this->once())
61-
->method('getParentIdsByChild')
62-
->willReturn([$parentId]);
63-
64-
$expected = array_merge($identities, [\Magento\Catalog\Model\Product::CACHE_TAG . '_' . $parentId]);
65-
66-
$this->assertEquals($expected, $this->model->getTags($product));
59+
$this->assertEquals($identities, $this->model->getTags($product));
6760
}
6861
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,4 @@
7777
</argument>
7878
</arguments>
7979
</virtualType>
80-
<type name="Magento\Catalog\Model\Product">
81-
<plugin name="clean_configurable_product_and_its_children_cache"
82-
type="Magento\ConfigurableProduct\Model\Plugin\CleanCache"/>
83-
</type>
8480
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,7 @@
178178
</argument>
179179
</arguments>
180180
</type>
181+
<type name="Magento\Catalog\Model\Product">
182+
<plugin name="product_identities_extender" type="Magento\ConfigurableProduct\Model\Plugin\ProductIdentitiesExtender" />
183+
</type>
181184
</config>

0 commit comments

Comments
 (0)