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 10, 2023
1 parent 5c01ad6 commit a2fbfee
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/module-elasticsuite-virtual-category/Helper/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Smile\ElasticsuiteVirtualCategory\Helper;

use Magento\Catalog\Api\CategoryRepositoryInterfaceFactory;
use Magento\Catalog\Api\Data\CategoryInterface;
use Smile\ElasticsuiteVirtualCategory\Model\Category\Attribute\VirtualRule\ReadHandler;

Expand Down Expand Up @@ -41,21 +42,29 @@ class Rule
*/
private $readHandler;

/**
* @var \Magento\Catalog\Api\CategoryRepositoryInterfaceFactory
*/
private $categoryRepository;

/**
* Provider constructor.
*
* @param \Magento\Framework\App\CacheInterface $cache Cache.
* @param \Magento\Customer\Model\Session $customerSession Customer session.
* @param ReadHandler $readHandler Rule read handler.
* @param \Magento\Framework\App\CacheInterface $cache Cache.
* @param \Magento\Customer\Model\Session $customerSession Customer session.
* @param ReadHandler $readHandler Rule read handler.
* @param \Magento\Catalog\Api\CategoryRepositoryInterfaceFactory $categoryRepository Category factory.
*/
public function __construct(
\Magento\Framework\App\CacheInterface $cache,
\Magento\Customer\Model\Session $customerSession,
ReadHandler $readHandler
ReadHandler $readHandler,
CategoryRepositoryInterfaceFactory $categoryRepository
) {
$this->cache = $cache;
$this->customerSession = $customerSession;
$this->readHandler = $readHandler;
$this->categoryRepository = $categoryRepository;
}

/**
Expand Down Expand Up @@ -83,7 +92,14 @@ public function loadUsingCache(CategoryInterface $category, $callback)
if ($data === false) {
$virtualRule = $category->getVirtualRule();

if (!is_object($virtualRule)) {
if (null === $virtualRule) {
// If virtual rule is null, probably the category himself was not properly loaded.
// So we load it through the repository and we ensure the readhandler will be called properly.
$repository = $this->categoryRepository->create();
$category = $repository->get($category->getId(), $category->getStoreId());
$category = $this->readHandler->execute($category);
$virtualRule = $category->getVirtualRule();
} elseif (!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.
Expand Down

0 comments on commit a2fbfee

Please sign in to comment.