Skip to content

Commit

Permalink
Fix Smile-SA#1985 Ensure proper loading of virtual categories rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed May 9, 2023
1 parent 1d350b2 commit ccc5646
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ 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);

$this->jsonResultFactory = $jsonResultFactory;
$this->layerResolver = $layerResolver;
$this->filterListPool = $filterListPool;
$this->categoryFactory = $categoryFactory;
$this->categoryRepository = $categoryRepository;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down
20 changes: 19 additions & 1 deletion src/module-elasticsuite-virtual-category/Helper/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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());
Expand Down

0 comments on commit ccc5646

Please sign in to comment.