Skip to content

Commit 7d785db

Browse files
Merge pull request #14 from magento-commerce/imported-magento-commerce-data-export-18
[Imported] Reindex variants by child ids
2 parents aa1b959 + 24580ea commit 7d785db

File tree

20 files changed

+605
-77
lines changed

20 files changed

+605
-77
lines changed

app/code/Magento/CatalogExportApi/Api/Data/ProductVariant.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class ProductVariant
3232
/** @var string */
3333
private $parentId;
3434

35+
/** @var string */
36+
private $productId;
37+
3538
/**
3639
* Get id
3740
*
@@ -94,4 +97,25 @@ public function setParentId(?string $parentId): void
9497
{
9598
$this->parentId = $parentId;
9699
}
100+
101+
/**
102+
* Get product id
103+
*
104+
* @return string
105+
*/
106+
public function getProductId(): ?string
107+
{
108+
return $this->productId;
109+
}
110+
111+
/**
112+
* Set product id
113+
*
114+
* @param string $productId
115+
* @return void
116+
*/
117+
public function setProductId(?string $productId): void
118+
{
119+
$this->productId = $productId;
120+
}
97121
}

app/code/Magento/ConfigurableProductDataExporter/Model/Provider/Product/ProductVariants.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,19 @@ public function __construct(
8383
public function get(array $values): array
8484
{
8585
$output = [];
86-
$parentIds = [];
86+
$childIds = [];
8787
foreach ($values as $value) {
88-
$parentIds[$value['parent_id']] = $value['parent_id'];
88+
$childIds[$value['product_id']] = $value['product_id'];
8989
}
9090

9191
try {
92-
$variants = $this->getVariants($parentIds);
92+
$variants = $this->getVariants($childIds);
9393
foreach ($variants as $id => $optionValues) {
9494
$output[] = [
9595
'id' => $id,
9696
'option_values' => $optionValues['optionValues'],
9797
'parent_id' => $optionValues['parentId'],
98+
'product_id' => $optionValues['childId']
9899
];
99100
}
100101
} catch (\Throwable $exception) {
@@ -107,18 +108,18 @@ public function get(array $values): array
107108
/**
108109
* Get configurable product variants
109110
*
110-
* @param array $parentIds
111+
* @param array $childIds
111112
* @return array
112113
* @throws \Zend_Db_Statement_Exception
113114
*/
114-
private function getVariants(array $parentIds): array
115+
private function getVariants(array $childIds): array
115116
{
116117
$variants = [];
117118
$idResolver = $this->idFactory->get('configurable');
118119
$optionValueResolver = $this->optionValueFactory->get('configurable');
119120

120121
$cursor = $this->resourceConnection->getConnection()->query(
121-
$this->variantsOptionValuesQuery->getQuery($parentIds)
122+
$this->variantsOptionValuesQuery->getQuery($childIds)
122123
);
123124
while ($row = $cursor->fetch()) {
124125
$id = $idResolver->resolve([
@@ -131,6 +132,7 @@ private function getVariants(array $parentIds): array
131132
));
132133
$optionValue = $optionValueResolver->resolve($row['parentId'], $row['attributeCode'], $optionValueUid);
133134
$variants[$id]['parentId'] = $row['parentId'];
135+
$variants[$id]['childId'] = $row['childId'];
134136
$variants[$id]['optionValues'][] = $optionValue;
135137
}
136138
return $variants;

app/code/Magento/ConfigurableProductDataExporter/Model/Query/LinkedAttributesQuery.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@ public function __construct(
3030
}
3131

3232
/**
33-
* Get query
33+
* Get linked attributes query
3434
*
3535
* @param int $productId
3636
* @return Select
3737
*/
3838
public function getQuery(int $productId): Select
3939
{
4040
$connection = $this->resourceConnection->getConnection();
41-
$joinField = $connection->getAutoIncrementField(
42-
$this->resourceConnection->getTableName('catalog_product_entity')
43-
);
44-
$select = $connection->select()
41+
return $connection->select()
4542
->from(
4643
['cpsl' => $this->resourceConnection->getTableName('catalog_product_super_link')],
4744
[]
@@ -56,18 +53,11 @@ public function getQuery(int $productId): Select
5653
'ea.attribute_id = cpsa.attribute_id',
5754
[]
5855
)
59-
->joinInner(
60-
['cpe' => $this->resourceConnection->getTableName('catalog_product_entity')],
61-
sprintf('cpe.%1$s = cpsa.product_id', $joinField),
62-
[]
63-
)
6456
->columns(
6557
[
66-
'parentId' => 'cpe.entity_id',
6758
'attributeCode' => 'ea.attribute_code',
6859
]
6960
)
7061
->where('cpsl.product_id = ?', $productId);
71-
return $select;
7262
}
7363
}

app/code/Magento/ConfigurableProductDataExporter/Model/Query/ProductVariantsQuery.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ public function getQuery(array $parentIds): Select
4747
['cpsa' => $this->resourceConnection->getTableName('catalog_product_super_attribute')],
4848
['attribute_id']
4949
)
50-
->where(\sprintf('cpsa.product_id IN (cpe.%s)', $joinField));
50+
->where(\sprintf('cpsa.product_id IN (cpep.%s)', $joinField));
5151
$select = $connection->select()
5252
->from(
53-
['cpe' => $this->resourceConnection->getTableName('catalog_product_entity')],
53+
['cpec' => $this->resourceConnection->getTableName('catalog_product_entity')],
5454
[]
5555
)
5656
->joinInner(
5757
['cpsl' => $this->resourceConnection->getTableName('catalog_product_super_link')],
58-
\sprintf('cpsl.parent_id = cpe.%s', $joinField),
58+
\sprintf('cpsl.product_id = cpec.entity_id'),
5959
[]
6060
)
6161
->joinInner(
62-
['cpec' => $this->resourceConnection->getTableName('catalog_product_entity')],
63-
'cpec.entity_id = cpsl.product_id',
62+
['cpep' => $this->resourceConnection->getTableName('catalog_product_entity')],
63+
\sprintf('cpep.%s = cpsl.parent_id', $joinField),
6464
[]
6565
)
6666
->joinInner(
@@ -79,14 +79,14 @@ public function getQuery(array $parentIds): Select
7979
)
8080
->columns(
8181
[
82-
'parentId' => 'cpe.entity_id',
82+
'parentId' => 'cpep.entity_id',
8383
'childId' => 'cpec.entity_id',
8484
'attributeId' => 'cpei.attribute_id',
8585
'attributeCode' => 'ea.attribute_code',
8686
'attributeValue' => 'cpei.value'
8787
]
8888
)
89-
->where('cpe.entity_id IN (?)', $parentIds);
89+
->where('cpec.entity_id IN (?)', $parentIds);
9090
return $select;
9191
}
9292
}

app/code/Magento/ConfigurableProductDataExporter/Plugin/ReindexVariants.php renamed to app/code/Magento/ConfigurableProductDataExporter/Plugin/ReindexVariantsAfterSave.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct;
1212
use Magento\ConfigurableProductDataExporter\Model\Query\LinkedAttributesQuery;
1313
use Magento\Framework\App\ResourceConnection;
14+
use Magento\Framework\Indexer\IndexerRegistry;
1415
use Magento\Framework\Model\AbstractModel;
1516
use Magento\ProductVariantDataExporter\Model\Indexer\ProductVariantFeedIndexer;
17+
use Magento\ProductVariantDataExporter\Model\Indexer\UpdateChangeLog;
1618

1719
/**
1820
* Plugin to trigger reindex on parent products, when a super attribute value is changed on a child product
1921
*/
20-
class ReindexVariants
22+
class ReindexVariantsAfterSave
2123
{
2224
/**
2325
* @var ResourceConnection
@@ -30,27 +32,35 @@ class ReindexVariants
3032
private $linkedAttributesQuery;
3133

3234
/**
33-
* @var ProductVariantFeedIndexer
35+
* @var IndexerRegistry
3436
*/
35-
private $feedIndexer;
37+
private $indexerRegistry;
38+
39+
/**
40+
* @var UpdateChangeLog
41+
*/
42+
private $updateChangeLog;
3643

3744
/**
3845
* @param ResourceConnection $resourceConnection
3946
* @param LinkedAttributesQuery $linkedAttributesQuery
40-
* @param ProductVariantFeedIndexer $feedIndexer
47+
* @param IndexerRegistry $indexerRegistry
48+
* @param UpdateChangeLog $updateChangeLog
4149
*/
4250
public function __construct(
4351
ResourceConnection $resourceConnection,
4452
LinkedAttributesQuery $linkedAttributesQuery,
45-
ProductVariantFeedIndexer $feedIndexer
53+
IndexerRegistry $indexerRegistry,
54+
UpdateChangeLog $updateChangeLog
4655
) {
4756
$this->resourceConnection = $resourceConnection;
4857
$this->linkedAttributesQuery = $linkedAttributesQuery;
49-
$this->feedIndexer = $feedIndexer;
58+
$this->indexerRegistry = $indexerRegistry;
59+
$this->updateChangeLog = $updateChangeLog;
5060
}
5161

5262
/**
53-
* Reindex parent product on change of super attribute value.
63+
* Reindex parent products on change of child product attribute value
5464
*
5565
* @param ResourceProduct $subject
5666
* @param ResourceProduct $result
@@ -66,18 +76,30 @@ public function afterSave(
6676
): ResourceProduct {
6777
if (\in_array($product->getTypeId(), [Type::TYPE_SIMPLE, Type::TYPE_VIRTUAL], true)) {
6878
$select = $this->linkedAttributesQuery->getQuery((int)$product->getId());
69-
$connection = $this->resourceConnection->getConnection();
70-
$configurableLinks = $connection->query($select)->fetchAll();
71-
$changedConfigurableIds = [];
72-
foreach ($configurableLinks as $link) {
73-
if ($product->getOrigData($link['attributeCode']) !== $product->getData($link['attributeCode'])) {
74-
$changedConfigurableIds[] = $link['parentId'];
79+
$linkedAttributes = $this->resourceConnection->getConnection()->fetchCol($select);
80+
foreach ($linkedAttributes as $linkAttribute) {
81+
if ($product->getOrigData($linkAttribute) !== $product->getData($linkAttribute)) {
82+
$this->reindexVariant((int)$product->getId());
83+
break;
7584
}
7685
}
77-
if (!empty($changedConfigurableIds)) {
78-
$this->feedIndexer->executeList(array_filter($changedConfigurableIds));
79-
}
8086
}
8187
return $result;
8288
}
89+
90+
/**
91+
* Reindex product variant
92+
*
93+
* @param int $id
94+
* @return void
95+
*/
96+
private function reindexVariant(int $id): void
97+
{
98+
$indexer = $this->indexerRegistry->get(ProductVariantFeedIndexer::INDEXER_ID);
99+
if ($indexer->isScheduled()) {
100+
$this->updateChangeLog->execute($indexer->getViewId(), [$id]);
101+
} else {
102+
$indexer->reindexRow($id);
103+
}
104+
}
83105
}

app/code/Magento/ConfigurableProductDataExporter/Test/Integration/ConfigurableProductVariantsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private function getExpectedProductVariants(ProductInterface $configurable, arra
144144
'id' => $id,
145145
'option_values' => $optionValues,
146146
'parent_id' => $configurable->getId(),
147+
'product_id' => $simple->getId(),
147148
'deleted' => false
148149
];
149150
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</arguments>
4949
</type>
5050
<type name="Magento\Catalog\Model\ResourceModel\Product">
51-
<plugin name="trigger_reindex_on_configurable_parent_product_variants"
52-
type="Magento\ConfigurableProductDataExporter\Plugin\ReindexVariants"/>
51+
<plugin name="reindex_configurable_variants_after_save"
52+
type="Magento\ConfigurableProductDataExporter\Plugin\ReindexVariantsAfterSave"/>
5353
</type>
5454
</config>

0 commit comments

Comments
 (0)