diff --git a/src/module-elasticsuite-catalog/Controller/Navigation/Filter/Ajax.php b/src/module-elasticsuite-catalog/Controller/Navigation/Filter/Ajax.php index e7470eec7..95c0cef63 100644 --- a/src/module-elasticsuite-catalog/Controller/Navigation/Filter/Ajax.php +++ b/src/module-elasticsuite-catalog/Controller/Navigation/Filter/Ajax.php @@ -43,24 +43,24 @@ class Ajax extends \Magento\Framework\App\Action\Action private $filterListPool; /** - * @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory + * @var \Magento\Catalog\Api\CategoryRepositoryInterfaceFactory */ - private $categoryFactory; + private $categoryRepository; /** * Constructor. * - * @param \Magento\Framework\App\Action\Context $context Controller action context. - * @param \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory JSON result factory. - * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver Layer resolver. - * @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory Category factory. - * @param \Magento\Catalog\Model\Layer\FilterList[] $filterListPool Filter list pool. + * @param \Magento\Framework\App\Action\Context $context Controller action context. + * @param \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory JSON result factory. + * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver Layer resolver. + * @param \Magento\Catalog\Api\CategoryRepositoryInterfaceFactory $categoryRepository Category factory. + * @param \Magento\Catalog\Model\Layer\FilterList[] $filterListPool Filter list pool. */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory, \Magento\Catalog\Model\Layer\Resolver $layerResolver, - \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory, + \Magento\Catalog\Api\CategoryRepositoryInterfaceFactory $categoryRepository, $filterListPool = [] ) { parent::__construct($context); @@ -68,7 +68,7 @@ public function __construct( $this->jsonResultFactory = $jsonResultFactory; $this->layerResolver = $layerResolver; $this->filterListPool = $filterListPool; - $this->categoryFactory = $categoryFactory; + $this->categoryRepository = $categoryRepository; } /** @@ -110,7 +110,11 @@ private function initLayer() $this->layerResolver->create($this->getLayerType()); if ($this->getRequest()->getParam('cat')) { - $category = $this->categoryFactory->create()->setId($this->getRequest()->getParam('cat')); + $category = $this->categoryRepository->create()->get( + $this->getRequest()->getParam('cat'), + $this->layerResolver->get()->getCurrentStore()->getId() + ); + $this->layerResolver->get()->setCurrentCategory($category); } diff --git a/src/module-elasticsuite-virtual-category/Helper/Rule.php b/src/module-elasticsuite-virtual-category/Helper/Rule.php index 2a4c62f41..d8cddbc23 100644 --- a/src/module-elasticsuite-virtual-category/Helper/Rule.php +++ b/src/module-elasticsuite-virtual-category/Helper/Rule.php @@ -15,6 +15,7 @@ namespace Smile\ElasticsuiteVirtualCategory\Helper; use Magento\Catalog\Api\Data\CategoryInterface; +use Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\ReadHandler; /** * Smile Elasticsuite virtual category cache helper. @@ -35,18 +36,26 @@ class Rule */ private $customerSession; + /** + * @var \Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\ReadHandler + */ + private $readHandler; + /** * Provider constructor. * * @param \Magento\Framework\App\CacheInterface $cache Cache. * @param \Magento\Customer\Model\Session $customerSession Customer session. + * @param ReadHandler $customerSession Rule read handler. */ public function __construct( \Magento\Framework\App\CacheInterface $cache, - \Magento\Customer\Model\Session $customerSession + \Magento\Customer\Model\Session $customerSession, + ReadHandler $readHandler ) { $this->cache = $cache; $this->customerSession = $customerSession; + $this->readHandler = $readHandler; } /** @@ -73,6 +82,15 @@ public function loadUsingCache(CategoryInterface $category, $callback) if ($data === false) { $virtualRule = $category->getVirtualRule(); + + if (!is_object($virtualRule)) { + // If virtual rule is not an object, probably the rule was not properly loaded. + // @see https://github.com/Smile-SA/elasticsuite/issues/1985. + // In such cases, we go through the readHandler once again. + $category = $this->readHandler->execute($category); + $virtualRule = $category->getVirtualRule(); + } + $data = call_user_func_array([$virtualRule, $callback], [$category]); $cacheData = serialize($data); $this->cache->save($cacheData, $cacheKey, $category->getCacheTags());