-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '27_product-variants' into 437_reindex_variants_by_child_id
- Loading branch information
Showing
13 changed files
with
460 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/code/Magento/ProductVariantDataExporter/Model/Indexer/UpdateChangeLog.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ProductVariantDataExporter\Model\Indexer; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Update indexer changelog | ||
*/ | ||
class UpdateChangeLog | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection, | ||
LoggerInterface $logger | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Update indexer change log | ||
* | ||
* @param string $viewId | ||
* @param array $ids | ||
*/ | ||
public function execute(string $viewId, array $ids): void | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$connection->beginTransaction(); | ||
try { | ||
foreach ($ids as $id) { | ||
$connection->insert($viewId . '_cl', ['entity_id' => $id]); | ||
} | ||
$connection->commit(); | ||
} catch (\Exception $e) { | ||
$connection->rollBack(); | ||
$this->logger->error( | ||
sprintf( | ||
'Failed to update change log of indexer %s. %s', | ||
$viewId, | ||
$e->getMessage() | ||
) | ||
); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/code/Magento/ProductVariantDataExporter/Model/Query/ProductRelationsQuery.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ProductVariantDataExporter\Model\Query; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
|
||
/** | ||
* Retrieve product relations | ||
*/ | ||
class ProductRelationsQuery | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resourceConnection; | ||
|
||
/** | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
|
||
/** | ||
* Return the parent ids of product relations | ||
* | ||
* @param int[] $ids | ||
* @return array | ||
*/ | ||
public function getRelationsParentIds(array $ids): array | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$select = $connection->select()->from( | ||
['cpr' => $this->resourceConnection->getTableName('catalog_product_relation')], | ||
[] | ||
)->join( | ||
['cpe' => $catalogProductTable = $this->resourceConnection->getTableName('catalog_product_entity')], | ||
\sprintf('cpe.%1$s = cpr.parent_id', $connection->getAutoIncrementField($catalogProductTable)), | ||
['entity_id'] | ||
)->where( | ||
sprintf( | ||
'cpe.entity_id IN ("%1$s") OR cpr.child_id IN ("%1$s")', | ||
\implode(",", $ids) | ||
) | ||
); | ||
return array_filter($connection->fetchCol($select)); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
app/code/Magento/ProductVariantDataExporter/Plugin/ReindexVariantsOnDelete.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ProductVariantDataExporter\Plugin; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product as ResourceProduct; | ||
use Magento\Framework\Indexer\IndexerRegistry; | ||
use Magento\Framework\Model\AbstractModel; | ||
use Magento\ProductVariantDataExporter\Model\Indexer\ProductVariantFeedIndexer; | ||
use Magento\ProductVariantDataExporter\Model\Indexer\UpdateChangeLog; | ||
use Magento\ProductVariantDataExporter\Model\Query\ProductRelationsQuery; | ||
|
||
/** | ||
* Plugin to trigger reindex on product variants upon product deletion | ||
*/ | ||
class ReindexVariantsOnDelete | ||
{ | ||
/** | ||
* @var IndexerRegistry | ||
*/ | ||
private $indexerRegistry; | ||
|
||
/** | ||
* @var UpdateChangeLog | ||
*/ | ||
private $updateChangeLog; | ||
|
||
/** | ||
* @var ProductRelationsQuery | ||
*/ | ||
private $productRelationsQuery; | ||
|
||
/** | ||
* @param IndexerRegistry $indexerRegistry | ||
* @param UpdateChangeLog $updateChangeLog | ||
* @param ProductRelationsQuery $productRelationsQuery | ||
*/ | ||
public function __construct( | ||
IndexerRegistry $indexerRegistry, | ||
UpdateChangeLog $updateChangeLog, | ||
ProductRelationsQuery $productRelationsQuery | ||
) { | ||
$this->indexerRegistry = $indexerRegistry; | ||
$this->updateChangeLog = $updateChangeLog; | ||
$this->productRelationsQuery = $productRelationsQuery; | ||
} | ||
|
||
/** | ||
* Reindex product variants on product deletion | ||
* | ||
* @param ResourceProduct $subject | ||
* @param \Closure $proceed | ||
* @param AbstractModel $product | ||
* @return ResourceProduct | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function aroundDelete( | ||
ResourceProduct $subject, | ||
\Closure $proceed, | ||
AbstractModel $product | ||
): ResourceProduct { | ||
$ids = $this->productRelationsQuery->getRelationsParentIds([$product->getId()]); | ||
$result = $proceed($product); | ||
if (!empty($ids)) { | ||
$this->reindexVariants($ids); | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Reindex product variants | ||
* | ||
* @param int[] $ids | ||
* @return void | ||
*/ | ||
private function reindexVariants(array $ids): void | ||
{ | ||
$indexer = $this->indexerRegistry->get(ProductVariantFeedIndexer::INDEXER_ID); | ||
if ($indexer->isScheduled()) { | ||
$this->updateChangeLog->execute($indexer->getViewId(), $ids); | ||
} else { | ||
$indexer->reindexList($ids); | ||
} | ||
} | ||
} |
Oops, something went wrong.