Skip to content

Commit

Permalink
Merge pull request #3355 from rbayet/fix-optimizer-select-category-pr…
Browse files Browse the repository at this point in the history
…eview-mandatory

[Optimizers] Fallback to root category for category preview
  • Loading branch information
rbayet authored Aug 28, 2024
2 parents dc398c5 + d678598 commit dc678bd
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use Magento\Backend\App\Action\Context;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Search\Model\QueryFactory;
use Magento\Store\Model\StoreManagerInterface;
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterface;
use Smile\ElasticsuiteCatalogOptimizer\Api\Data\OptimizerInterfaceFactory;
use Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\PreviewFactory;
Expand Down Expand Up @@ -68,6 +70,11 @@ class Preview extends Action
*/
private $queryFactory;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var \Smile\ElasticsuiteCore\Api\Search\ContextInterface
*/
Expand All @@ -83,6 +90,7 @@ class Preview extends Action
* @param ContainerConfigurationFactory $containerConfigFactory Container Configuration Factory
* @param JsonHelper $jsonHelper JSON Helper.
* @param QueryFactory $queryFactory Query Factory.
* @param StoreManagerInterface $storeManager Store Manager.
* @param ContextInterface $searchContext Search context.
*/
public function __construct(
Expand All @@ -93,6 +101,7 @@ public function __construct(
ContainerConfigurationFactory $containerConfigFactory,
JsonHelper $jsonHelper,
QueryFactory $queryFactory,
StoreManagerInterface $storeManager,
ContextInterface $searchContext
) {
parent::__construct($context);
Expand All @@ -103,6 +112,7 @@ public function __construct(
$this->containerConfigFactory = $containerConfigFactory;
$this->jsonHelper = $jsonHelper;
$this->queryFactory = $queryFactory;
$this->storeManager = $storeManager;
$this->searchContext = $searchContext;
}

Expand Down Expand Up @@ -131,15 +141,15 @@ protected function _isAllowed()
/**
* Load and initialize the preview model.
*
* @return \Smile\ElasticsuiteVirtualCategory\Model\Preview
* @return \Smile\ElasticsuiteCatalogOptimizer\Model\Optimizer\Preview
*/
private function getPreviewObject()
{
$optimizer = $this->getOptimizer();
$pageSize = $this->getPageSize();
$queryText = $this->getQueryText();
$category = $this->getCategory();
$containerConfig = $this->getContainerConfiguration();
$category = $this->getCategory($containerConfig);

$this->updateSearchContext($this->getStoreId(), $category, $queryText);

Expand Down Expand Up @@ -172,15 +182,22 @@ private function getOptimizer()
/**
* Load current category using the request params.
*
* @return CategoryInterface
* @param ContainerConfigurationInterface $containerConfig Container config.
*
* @return CategoryInterface|null
* @throws NoSuchEntityException
*/
private function getCategory()
private function getCategory($containerConfig)
{
$storeId = $this->getStoreId();
$categoryId = $this->getCategoryId();
$category = null;

if ($this->getCategoryId()) {
if ((null === $categoryId) && ('catalog_view_container' === $containerConfig->getName())) {
$categoryId = $this->storeManager->getStore($storeId)->getRootCategoryId();
}

if ($categoryId) {
$category = $this->categoryRepository->get($categoryId, $storeId);
}

Expand Down Expand Up @@ -232,7 +249,7 @@ private function getQueryText()
/**
* Return the category to preview.
*
* @return int
* @return int|null
*/
private function getCategoryId()
{
Expand Down

0 comments on commit dc678bd

Please sign in to comment.