Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEG-143: Trigger re-caching when child variant is edited #15 #16

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
51 changes: 51 additions & 0 deletions Helper/ProductIndexer.php
aligent-lturner marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Aligent\PrerenderIo\Helper;

use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

/**
* ProductIndexer helper class
*/
class ProductIndexer
{
/**
* @var Configurable
*/
private Configurable $configurable;

/**
* @var ProductFactory
*/
private ProductFactory $productFactory;

/**
* @param Configurable $configurable
* @param ProductFactory $productFactory
*/
public function __construct(
Configurable $configurable,
ProductFactory $productFactory
) {
$this->configurable = $configurable;
$this->productFactory = $productFactory;
}

/**
* Returns parent entity id(s)
*
* @param $simpleProductId
* @return array
*/
public function getParentEntityId($simpleProductId): array
{
$parentIds = [];
$simpleProduct = $this->productFactory->create()->load($simpleProductId);
if ($simpleProduct->getTypeId() == 'simple') {
aligent-lturner marked this conversation as resolved.
Show resolved Hide resolved
// Get the parent IDs of the simple product (configurable product IDs)
$parentIds = $this->configurable->getParentIdsByChild($simpleProductId);
}
return $parentIds;
}
}
14 changes: 14 additions & 0 deletions Model/Indexer/Category/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Aligent\PrerenderIo\Api\PrerenderClientInterface;
use Aligent\PrerenderIo\Helper\Config;
use Aligent\PrerenderIo\Helper\ProductIndexer as ProductIndexerHelper;
use Aligent\PrerenderIo\Model\Indexer\DataProvider\ProductCategories;
use Aligent\PrerenderIo\Model\Url\GetUrlsForCategories;
use Magento\Framework\App\DeploymentConfig;
Expand Down Expand Up @@ -38,6 +39,10 @@ class ProductIndexer implements IndexerActionInterface, MviewActionInterface, Di
private DeploymentConfig $eploymentConfig;
/** @var Config */
private Config $prerenderConfigHelper;
/**
* @var ProductIndexerHelper
*/
private ProductIndexerHelper $productIndexerHelper;
/** @var int|null */
private ?int $batchSize;

Expand All @@ -58,6 +63,7 @@ public function __construct(
PrerenderClientInterface $prerenderClient,
DeploymentConfig $deploymentConfig,
Config $prerenderConfigHelper,
ProductIndexerHelper $productIndexerHelper,
?int $batchSize = 1000
) {
$this->dimensionProvider = $dimensionProvider;
Expand All @@ -67,6 +73,7 @@ public function __construct(
$this->deploymentConfig = $deploymentConfig;
$this->batchSize = $batchSize;
$this->prerenderConfigHelper = $prerenderConfigHelper;
$this->productIndexerHelper = $this->productIndexerHelper;
}

/**
Expand Down Expand Up @@ -144,6 +151,13 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds):
}

$entityIds = iterator_to_array($entityIds);
// Include configurable product id(s) if the edited product is simple
foreach ($entityIds as $entityId) {
$parentEntityIds = $this->productIndexerHelper->getParentEntityId($entityId);
if (!empty($parentEntityIds)) {
$entityIds = array_merge($entityIds, $parentEntityIds);
aligent-lturner marked this conversation as resolved.
Show resolved Hide resolved
}
}
// get list of category ids for the products
$categoryIds = $this->productCategoriesDataProvider->getCategoryIdsForProducts($entityIds, $storeId);

Expand Down
14 changes: 14 additions & 0 deletions Model/Indexer/Product/ProductIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Aligent\PrerenderIo\Api\PrerenderClientInterface;
use Aligent\PrerenderIo\Helper\Config;
use Aligent\PrerenderIo\Helper\ProductIndexer as ProductIndexerHelper;
use Aligent\PrerenderIo\Model\Url\GetUrlsForProducts;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Exception\FileSystemException;
Expand All @@ -35,6 +36,10 @@ class ProductIndexer implements IndexerActionInterface, MviewActionInterface, Di
private DeploymentConfig $eploymentConfig;
/** @var Config */
private Config $prerenderConfigHelper;
/**
* @var ProductIndexerHelper
*/
private ProductIndexerHelper $productIndexerHelper;
/** @var int|null */
private ?int $batchSize;

Expand All @@ -53,6 +58,7 @@ public function __construct(
PrerenderClientInterface $prerenderClient,
DeploymentConfig $deploymentConfig,
Config $prerenderConfigHelper,
ProductIndexerHelper $productIndexerHelper,
?int $batchSize = 1000
) {
$this->dimensionProvider = $dimensionProvider;
Expand All @@ -61,6 +67,7 @@ public function __construct(
$this->deploymentConfig = $deploymentConfig;
$this->batchSize = $batchSize;
$this->prerenderConfigHelper = $prerenderConfigHelper;
$this->productIndexerHelper = $productIndexerHelper;
}

/**
Expand Down Expand Up @@ -138,6 +145,13 @@ public function executeByDimensions(array $dimensions, \Traversable $entityIds):
}

$entityIds = iterator_to_array($entityIds);
// Include configurable product id(s) if the edited product is simple
aligent-lturner marked this conversation as resolved.
Show resolved Hide resolved
foreach ($entityIds as $entityId) {
$parentEntityIds = $this->productIndexerHelper->getParentEntityId($entityId);
if (!empty($parentEntityIds)) {
$entityIds = array_merge($entityIds, $parentEntityIds);
}
}
// get urls for the products
$urls = $this->getUrlsForProducts->execute($entityIds, $storeId);

Expand Down
Loading