Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
776211f
Product variants
Nov 11, 2020
f60f2dd
Merge branch '27_product-variants' of https://github.com/bricht/comme…
RuslanKostiv1 Nov 11, 2020
9b5a9f0
Product variants
Nov 12, 2020
06df519
Product variants
Nov 12, 2020
459f3d4
Merge branch 'main' into 27_product-variants
Nov 12, 2020
affb111
Product variants
Nov 12, 2020
7ce062c
Merge branch '27_product-variants' of https://github.com/bricht/comme…
RuslanKostiv1 Nov 12, 2020
c9a6013
27: Product variants
RuslanKostiv1 Nov 12, 2020
d35fd8b
Product variants
Nov 12, 2020
46d2d59
Merge branch '27_product-variants' of https://github.com/bricht/comme…
RuslanKostiv1 Nov 12, 2020
5ef8cfe
Product variants
Nov 13, 2020
47d550d
Revert "Product variants"
Nov 13, 2020
9c44e5e
Product variants
Nov 13, 2020
d8aca1b
Product variants
Nov 13, 2020
ebbf259
Product variants
Nov 16, 2020
1b67788
Merge branch 'main' into 27_product-variants
Nov 16, 2020
f8076b3
Product variants
Nov 16, 2020
5f7323c
Product variant improvements
Nov 16, 2020
e02f9bd
Product variants
Nov 16, 2020
8978778
Product variants
Nov 16, 2020
44bb5e8
Revert "Product variants"
Nov 16, 2020
517cb80
Product variant improvements
Nov 17, 2020
eeb717f
Product variant improvements
Nov 17, 2020
3cfd343
Merge branch '27_product-variants' of https://github.com/bricht/comme…
RuslanKostiv1 Nov 18, 2020
65f3bab
27: Product variants
RuslanKostiv1 Nov 18, 2020
70bac0b
Product variant improvements
Nov 19, 2020
24d7061
Revert "27: Product variants"
Nov 25, 2020
2997c98
Product variants
Nov 25, 2020
2d9d3fe
Merge branch '27_product-variants' into 437_reindex_variants_by_child_id
Nov 30, 2020
e224379
Reindex Product Variants by Child ID
Nov 30, 2020
6bfbc61
Reindex Product Variants by Child ID
Nov 30, 2020
8829ae0
Merge remote-tracking branch 'commerce/main' into 437_reindex_variant…
RuslanKostiv1 Dec 3, 2020
047460c
merge conflict fixes
RuslanKostiv1 Dec 4, 2020
24580ea
Merge remote-tracking branch 'commerce/main' into 437_reindex_variant…
RuslanKostiv1 Dec 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/code/Magento/CatalogExportApi/Api/Data/ProductVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ProductVariant
/** @var string */
private $parentId;

/** @var string */
private $productId;

/**
* Get id
*
Expand Down Expand Up @@ -94,4 +97,25 @@ public function setParentId(?string $parentId): void
{
$this->parentId = $parentId;
}

/**
* Get product id
*
* @return string
*/
public function getProductId(): ?string
{
return $this->productId;
}

/**
* Set product id
*
* @param string $productId
* @return void
*/
public function setProductId(?string $productId): void
{
$this->productId = $productId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,19 @@ public function __construct(
public function get(array $values): array
{
$output = [];
$parentIds = [];
$childIds = [];
foreach ($values as $value) {
$parentIds[$value['parent_id']] = $value['parent_id'];
$childIds[$value['product_id']] = $value['product_id'];
}

try {
$variants = $this->getVariants($parentIds);
$variants = $this->getVariants($childIds);
foreach ($variants as $id => $optionValues) {
$output[] = [
'id' => $id,
'option_values' => $optionValues['optionValues'],
'parent_id' => $optionValues['parentId'],
'product_id' => $optionValues['childId']
];
}
} catch (\Throwable $exception) {
Expand All @@ -107,18 +108,18 @@ public function get(array $values): array
/**
* Get configurable product variants
*
* @param array $parentIds
* @param array $childIds
* @return array
* @throws \Zend_Db_Statement_Exception
*/
private function getVariants(array $parentIds): array
private function getVariants(array $childIds): array
{
$variants = [];
$idResolver = $this->idFactory->get('configurable');
$optionValueResolver = $this->optionValueFactory->get('configurable');

$cursor = $this->resourceConnection->getConnection()->query(
$this->variantsOptionValuesQuery->getQuery($parentIds)
$this->variantsOptionValuesQuery->getQuery($childIds)
);
while ($row = $cursor->fetch()) {
$id = $idResolver->resolve([
Expand All @@ -131,6 +132,7 @@ private function getVariants(array $parentIds): array
));
$optionValue = $optionValueResolver->resolve($row['parentId'], $row['attributeCode'], $optionValueUid);
$variants[$id]['parentId'] = $row['parentId'];
$variants[$id]['childId'] = $row['childId'];
$variants[$id]['optionValues'][] = $optionValue;
}
return $variants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,15 @@ public function __construct(
}

/**
* Get query
* Get linked attributes query
*
* @param int $productId
* @return Select
*/
public function getQuery(int $productId): Select
{
$connection = $this->resourceConnection->getConnection();
$joinField = $connection->getAutoIncrementField(
$this->resourceConnection->getTableName('catalog_product_entity')
);
$select = $connection->select()
return $connection->select()
->from(
['cpsl' => $this->resourceConnection->getTableName('catalog_product_super_link')],
[]
Expand All @@ -56,18 +53,11 @@ public function getQuery(int $productId): Select
'ea.attribute_id = cpsa.attribute_id',
[]
)
->joinInner(
['cpe' => $this->resourceConnection->getTableName('catalog_product_entity')],
sprintf('cpe.%1$s = cpsa.product_id', $joinField),
[]
)
->columns(
[
'parentId' => 'cpe.entity_id',
'attributeCode' => 'ea.attribute_code',
]
)
->where('cpsl.product_id = ?', $productId);
return $select;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public function getQuery(array $parentIds): Select
['cpsa' => $this->resourceConnection->getTableName('catalog_product_super_attribute')],
['attribute_id']
)
->where(\sprintf('cpsa.product_id IN (cpe.%s)', $joinField));
->where(\sprintf('cpsa.product_id IN (cpep.%s)', $joinField));
$select = $connection->select()
->from(
['cpe' => $this->resourceConnection->getTableName('catalog_product_entity')],
['cpec' => $this->resourceConnection->getTableName('catalog_product_entity')],
[]
)
->joinInner(
['cpsl' => $this->resourceConnection->getTableName('catalog_product_super_link')],
\sprintf('cpsl.parent_id = cpe.%s', $joinField),
\sprintf('cpsl.product_id = cpec.entity_id'),
[]
)
->joinInner(
['cpec' => $this->resourceConnection->getTableName('catalog_product_entity')],
'cpec.entity_id = cpsl.product_id',
['cpep' => $this->resourceConnection->getTableName('catalog_product_entity')],
\sprintf('cpep.%s = cpsl.parent_id', $joinField),
[]
)
->joinInner(
Expand All @@ -79,14 +79,14 @@ public function getQuery(array $parentIds): Select
)
->columns(
[
'parentId' => 'cpe.entity_id',
'parentId' => 'cpep.entity_id',
'childId' => 'cpec.entity_id',
'attributeId' => 'cpei.attribute_id',
'attributeCode' => 'ea.attribute_code',
'attributeValue' => 'cpei.value'
]
)
->where('cpe.entity_id IN (?)', $parentIds);
->where('cpec.entity_id IN (?)', $parentIds);
return $select;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct;
use Magento\ConfigurableProductDataExporter\Model\Query\LinkedAttributesQuery;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Model\AbstractModel;
use Magento\ProductVariantDataExporter\Model\Indexer\ProductVariantFeedIndexer;
use Magento\ProductVariantDataExporter\Model\Indexer\UpdateChangeLog;

/**
* Plugin to trigger reindex on parent products, when a super attribute value is changed on a child product
*/
class ReindexVariants
class ReindexVariantsAfterSave
{
/**
* @var ResourceConnection
Expand All @@ -30,27 +32,35 @@ class ReindexVariants
private $linkedAttributesQuery;

/**
* @var ProductVariantFeedIndexer
* @var IndexerRegistry
*/
private $feedIndexer;
private $indexerRegistry;

/**
* @var UpdateChangeLog
*/
private $updateChangeLog;

/**
* @param ResourceConnection $resourceConnection
* @param LinkedAttributesQuery $linkedAttributesQuery
* @param ProductVariantFeedIndexer $feedIndexer
* @param IndexerRegistry $indexerRegistry
* @param UpdateChangeLog $updateChangeLog
*/
public function __construct(
ResourceConnection $resourceConnection,
LinkedAttributesQuery $linkedAttributesQuery,
ProductVariantFeedIndexer $feedIndexer
IndexerRegistry $indexerRegistry,
UpdateChangeLog $updateChangeLog
) {
$this->resourceConnection = $resourceConnection;
$this->linkedAttributesQuery = $linkedAttributesQuery;
$this->feedIndexer = $feedIndexer;
$this->indexerRegistry = $indexerRegistry;
$this->updateChangeLog = $updateChangeLog;
}

/**
* Reindex parent product on change of super attribute value.
* Reindex parent products on change of child product attribute value
*
* @param ResourceProduct $subject
* @param ResourceProduct $result
Expand All @@ -66,18 +76,30 @@ public function afterSave(
): ResourceProduct {
if (\in_array($product->getTypeId(), [Type::TYPE_SIMPLE, Type::TYPE_VIRTUAL], true)) {
$select = $this->linkedAttributesQuery->getQuery((int)$product->getId());
$connection = $this->resourceConnection->getConnection();
$configurableLinks = $connection->query($select)->fetchAll();
$changedConfigurableIds = [];
foreach ($configurableLinks as $link) {
if ($product->getOrigData($link['attributeCode']) !== $product->getData($link['attributeCode'])) {
$changedConfigurableIds[] = $link['parentId'];
$linkedAttributes = $this->resourceConnection->getConnection()->fetchCol($select);
foreach ($linkedAttributes as $linkAttribute) {
if ($product->getOrigData($linkAttribute) !== $product->getData($linkAttribute)) {
$this->reindexVariant((int)$product->getId());
break;
}
}
if (!empty($changedConfigurableIds)) {
$this->feedIndexer->executeList(array_filter($changedConfigurableIds));
}
}
return $result;
}

/**
* Reindex product variant
*
* @param int $id
* @return void
*/
private function reindexVariant(int $id): void
{
$indexer = $this->indexerRegistry->get(ProductVariantFeedIndexer::INDEXER_ID);
if ($indexer->isScheduled()) {
$this->updateChangeLog->execute($indexer->getViewId(), [$id]);
} else {
$indexer->reindexRow($id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ private function getExpectedProductVariants(ProductInterface $configurable, arra
'id' => $id,
'option_values' => $optionValues,
'parent_id' => $configurable->getId(),
'product_id' => $simple->getId(),
'deleted' => false
];
}
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/ConfigurableProductDataExporter/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</arguments>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="trigger_reindex_on_configurable_parent_product_variants"
type="Magento\ConfigurableProductDataExporter\Plugin\ReindexVariants"/>
<plugin name="reindex_configurable_variants_after_save"
type="Magento\ConfigurableProductDataExporter\Plugin\ReindexVariantsAfterSave"/>
</type>
</config>
Loading