Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit e31b5b7

Browse files
committed
MAGETWO-72370: Configurable product shows up on category page on storefront when child products are disabled via Mass Action
1 parent e442de5 commit e31b5b7

File tree

4 files changed

+143
-19
lines changed

4 files changed

+143
-19
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\EntityManager\MetadataPool;
1314

1415
/**
1516
* Abstract action reindex class
@@ -70,25 +71,33 @@ abstract class AbstractAction
7071
*/
7172
private $cacheCleaner;
7273

74+
/**
75+
* @var MetadataPool
76+
*/
77+
private $metadataPool;
78+
7379
/**
7480
* @param ResourceConnection $resource
7581
* @param \Magento\CatalogInventory\Model\ResourceModel\Indexer\StockFactory $indexerFactory
7682
* @param \Magento\Catalog\Model\Product\Type $catalogProductType
7783
* @param \Magento\Framework\Indexer\CacheContext $cacheContext
7884
* @param \Magento\Framework\Event\ManagerInterface $eventManager
85+
* @param MetadataPool|null $metadataPool
7986
*/
8087
public function __construct(
8188
ResourceConnection $resource,
8289
\Magento\CatalogInventory\Model\ResourceModel\Indexer\StockFactory $indexerFactory,
8390
\Magento\Catalog\Model\Product\Type $catalogProductType,
8491
\Magento\Framework\Indexer\CacheContext $cacheContext,
85-
\Magento\Framework\Event\ManagerInterface $eventManager
92+
\Magento\Framework\Event\ManagerInterface $eventManager,
93+
MetadataPool $metadataPool = null
8694
) {
8795
$this->_resource = $resource;
8896
$this->_indexerFactory = $indexerFactory;
8997
$this->_catalogProductType = $catalogProductType;
9098
$this->cacheContext = $cacheContext;
9199
$this->eventManager = $eventManager;
100+
$this->metadataPool = $metadataPool ?: ObjectManager::getInstance()->get(MetadataPool::class);
92101
}
93102

94103
/**
@@ -154,10 +163,15 @@ protected function _getTable($entityName)
154163
public function getRelationsByChild($childIds)
155164
{
156165
$connection = $this->_getConnection();
157-
$select = $connection->select()
158-
->from($this->_getTable('catalog_product_relation'), 'parent_id')
159-
->where('child_id IN(?)', $childIds);
160-
166+
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
167+
->getLinkField();
168+
$select = $connection->select()->from(
169+
['cpe' => $this->_getTable('catalog_product_entity')],
170+
'entity_id'
171+
)->join(
172+
['relation' => $this->_getTable('catalog_product_relation')],
173+
'relation.parent_id = cpe.' . $linkField
174+
)->where('child_id IN(?)', $childIds);
161175
return $connection->fetchCol($select);
162176
}
163177

@@ -230,7 +244,8 @@ protected function _reindexRows($productIds = [])
230244
if (!is_array($productIds)) {
231245
$productIds = [$productIds];
232246
}
233-
247+
$parentIds = $this->getRelationsByChild($productIds);
248+
$productIds = $parentIds ? array_unique(array_merge($parentIds, $productIds)) : $productIds;
234249
$this->getCacheCleaner()->clean($productIds, function () use ($productIds) {
235250
$this->doReindex($productIds);
236251
});
@@ -248,13 +263,10 @@ private function doReindex($productIds = [])
248263
{
249264
$connection = $this->_getConnection();
250265

251-
$parentIds = $this->getRelationsByChild($productIds);
252-
$processIds = $parentIds ? array_merge($parentIds, $productIds) : $productIds;
253-
254266
// retrieve product types by processIds
255267
$select = $connection->select()
256268
->from($this->_getTable('catalog_product_entity'), ['entity_id', 'type_id'])
257-
->where('entity_id IN(?)', $processIds);
269+
->where('entity_id IN(?)', $productIds);
258270
$pairs = $connection->fetchPairs($select);
259271

260272
$byType = [];
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogInventory\Model\Plugin;
7+
8+
use Magento\Catalog\Model\Product\Action as ProductAction;
9+
10+
/**
11+
* Plugin for Magento\Catalog\Model\Product\Action
12+
*/
13+
class ReindexUpdatedProducts
14+
{
15+
/**
16+
* @var \Magento\CatalogInventory\Model\Indexer\Stock\Processor
17+
*/
18+
private $indexerProcessor;
19+
20+
/**
21+
* @param \Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor
22+
*/
23+
public function __construct(\Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor)
24+
{
25+
$this->indexerProcessor = $indexerProcessor;
26+
}
27+
28+
/**
29+
* Reindex on product attribute mass change
30+
*
31+
* @param ProductAction $subject
32+
* @param ProductAction $action
33+
* @param array $productIds
34+
* @return ProductAction
35+
*
36+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
37+
*/
38+
public function afterUpdateAttributes(
39+
ProductAction $subject,
40+
ProductAction $action,
41+
$productIds
42+
) {
43+
$this->indexerProcessor->reindexList(array_unique($productIds));
44+
return $action;
45+
}
46+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
<type name="Magento\Catalog\Model\Product">
7979
<plugin name="catalogInventoryAfterLoad" type="Magento\CatalogInventory\Model\Plugin\AfterProductLoad"/>
8080
</type>
81+
<type name="Magento\Catalog\Model\Product\Action">
82+
<plugin name="ReindexUpdatedProducts" type="Magento\CatalogInventory\Model\Plugin\ReindexUpdatedProducts"/>
83+
</type>
8184
<type name="Magento\CatalogInventory\Setup\UpgradeData">
8285
<arguments>
8386
<argument name="indexerProcessor" xsi:type="object">Magento\CatalogInventory\Model\Indexer\Stock\Processor</argument>

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/ActionTest.php

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ class ActionTest extends \PHPUnit\Framework\TestCase
1919
*/
2020
private $objectManager;
2121

22-
public static function setUpBeforeClass()
23-
{
24-
/** @var \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry */
25-
$indexerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
26-
->get(\Magento\Framework\Indexer\IndexerRegistry::class);
27-
$indexerRegistry->get(Fulltext::INDEXER_ID)->setScheduled(true);
28-
}
29-
3022
protected function setUp()
3123
{
3224
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
@@ -47,6 +39,10 @@ protected function setUp()
4739
*/
4840
public function testUpdateWebsites()
4941
{
42+
/** @var \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry */
43+
$indexerRegistry = $this->objectManager->get(\Magento\Framework\Indexer\IndexerRegistry::class);
44+
$indexerRegistry->get(Fulltext::INDEXER_ID)->setScheduled(true);
45+
5046
/** @var \Magento\Store\Api\WebsiteRepositoryInterface $websiteRepository */
5147
$websiteRepository = $this->objectManager->create(\Magento\Store\Api\WebsiteRepositoryInterface::class);
5248

@@ -84,7 +80,74 @@ public function testUpdateWebsites()
8480
}
8581
}
8682

87-
public static function tearDownAfterClass()
83+
/**
84+
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
85+
* @magentoAppArea adminhtml
86+
* @param string $status
87+
* @param string $productsCount
88+
* @dataProvider updateAttributesDataProvider
89+
*/
90+
public function testUpdateAttributes($status, $productsCount)
91+
{
92+
/** @var \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry */
93+
$indexerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
94+
->get(\Magento\Framework\Indexer\IndexerRegistry::class);
95+
$indexerRegistry->get(Fulltext::INDEXER_ID)->setScheduled(false);
96+
97+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
98+
$productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
99+
100+
/** @var \Magento\Catalog\Model\Product $product */
101+
$product = $productRepository->get('configurable');
102+
$productAttributesOptions = $product->getExtensionAttributes()->getConfigurableProductLinks();
103+
$attrData = ['status' => $status];
104+
$configurableOptionsId = [];
105+
if (isset($productAttributesOptions)) {
106+
foreach ($productAttributesOptions as $configurableOption) {
107+
$configurableOptionsId[] = $configurableOption;
108+
}
109+
}
110+
$this->action->updateAttributes($configurableOptionsId, $attrData, $product->getStoreId());
111+
112+
$categoryFactory = $this->objectManager->create(\Magento\Catalog\Model\CategoryFactory::class);
113+
/** @var \Magento\Catalog\Block\Product\ListProduct $listProduct */
114+
$listProduct = $this->objectManager->create(\Magento\Catalog\Block\Product\ListProduct::class);
115+
$category = $categoryFactory->create()->load(2);
116+
$layer = $listProduct->getLayer();
117+
$layer->setCurrentCategory($category);
118+
$productCollection = $layer->getProductCollection();
119+
$productCollection->joinField(
120+
'qty',
121+
'cataloginventory_stock_status',
122+
'qty',
123+
'product_id=entity_id',
124+
'{{table}}.stock_id=1',
125+
'left'
126+
);
127+
128+
$this->assertEquals($productsCount, $productCollection->count());
129+
}
130+
131+
/**
132+
* DataProvider for testUpdateAttributes
133+
*
134+
* @return array
135+
*/
136+
public function updateAttributesDataProvider()
137+
{
138+
return [
139+
[
140+
'status' => 2,
141+
'expected_count' => 0
142+
],
143+
[
144+
'status' => 1,
145+
'expected_count' => 1
146+
],
147+
];
148+
}
149+
150+
public function tearDown()
88151
{
89152
/** @var \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry */
90153
$indexerRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()

0 commit comments

Comments
 (0)