Skip to content

Commit

Permalink
Merge pull request #3386 from rbayet/2948-elasticsuite-catalog-fallba…
Browse files Browse the repository at this point in the history
…ck-to-last-valid-product-positions-2.10-fix

[Catalog] Fix #2948 fallback to the last known valid product positions
  • Loading branch information
rbayet authored Sep 16, 2024
2 parents 0c91e43 + 2143600 commit d3f8eae
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
*/
class SaveProductsPositions extends AbstractIndexerPlugin
{
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
protected $messageManager;

/**
* @var \Magento\Framework\Json\Helper\Data
*/
Expand All @@ -44,17 +49,20 @@ class SaveProductsPositions extends AbstractIndexerPlugin
* @param \Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position $saveHandler Product position
* save handler.
* @param \Magento\Framework\Json\Helper\Data $jsonHelper JSON Helper.
* @param \Magento\Framework\Message\ManagerInterface $messageManager Message Manager.
*/
public function __construct(
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
FullIndexer $fullIndexer,
\Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position $saveHandler,
\Magento\Framework\Json\Helper\Data $jsonHelper
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\Framework\Message\ManagerInterface $messageManager
) {
parent::__construct($indexerRegistry, $fullIndexer);

$this->jsonHelper = $jsonHelper;
$this->saveHandler = $saveHandler;
$this->messageManager = $messageManager;
}

/**
Expand Down Expand Up @@ -145,17 +153,23 @@ private function getAffectedProductIds($category)
*
* @param \Magento\Catalog\Model\Category $category Category
*
* @return array
* @return $this
*/
private function unserializeProductPositions(\Magento\Catalog\Model\Category $category)
{
// Get product positions from the category.
$productPositions = $category->getSortedProducts() ? $category->getSortedProducts() : [];

if (is_string($productPositions)) {
try {
$productPositions = $this->jsonHelper->jsonDecode($productPositions);
} catch (\Exception $e) {
$productPositions = [];
$this->messageManager->addWarningMessage(
__('Something went wrong while saving your product positions, they have been switched back to their last known state.')
);

// Fallback to the last known valid product positions.
$productPositions = $this->saveHandler->getProductPositionsByCategory($category);
}
}

Expand Down

0 comments on commit d3f8eae

Please sign in to comment.