From 214360032dad2d2ab75d89c9c474d1291008217b Mon Sep 17 00:00:00 2001 From: Vadym Honcharuk Date: Wed, 4 Sep 2024 15:48:36 +0300 Subject: [PATCH] [Catalog] Fix #2948 fallback to the last known valid product positions --- .../Category/SaveProductsPositions.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php b/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php index 331ac5220..f7b3d9df2 100644 --- a/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php +++ b/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php @@ -26,6 +26,11 @@ */ class SaveProductsPositions extends AbstractIndexerPlugin { + /** + * @var \Magento\Framework\Message\ManagerInterface + */ + protected $messageManager; + /** * @var \Magento\Framework\Json\Helper\Data */ @@ -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; } /** @@ -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); } }