Skip to content

Commit

Permalink
Ensure current category is properly injected into search context when…
Browse files Browse the repository at this point in the history
… doing an API request on catalog_view_container.
  • Loading branch information
romainruaud committed Jan 15, 2019
1 parent 5eeed91 commit 31aa0f0
Showing 1 changed file with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Smile\ElasticsuiteCatalog\Plugin\Search;

use Smile\ElasticsuiteCore\Api\Search\ContextInterface;
use Smile\ElasticsuiteCore\Model\Search\RequestMapper;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
Expand Down Expand Up @@ -58,21 +59,37 @@ class RequestMapperPlugin
*/
private $mappingHelper;

/**
* @var \Smile\ElasticsuiteCore\Api\Search\ContextInterface
*/
private $searchContext;

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

/**
* Constructor.
*
* @param \Magento\Customer\Model\Session $customerSession Customer session.
* @param \Magento\Store\Model\StoreManagerInterface $storeManager Store manager.
* @param \Smile\ElasticsuiteCore\Helper\Mapping $mappingHelper Mapping helper.
* @param \Magento\Customer\Model\Session $customerSession Customer session.
* @param \Magento\Store\Model\StoreManagerInterface $storeManager Store manager.
* @param \Smile\ElasticsuiteCore\Helper\Mapping $mappingHelper Mapping helper.
* @param \Smile\ElasticsuiteCore\Api\Search\ContextInterface $searchContext Search context.
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository Category Repository.
*/
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Smile\ElasticsuiteCore\Helper\Mapping $mappingHelper
\Smile\ElasticsuiteCore\Helper\Mapping $mappingHelper,
\Smile\ElasticsuiteCore\Api\Search\ContextInterface $searchContext,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
) {
$this->customerSession = $customerSession;
$this->storeManager = $storeManager;
$this->mappingHelper = $mappingHelper;
$this->customerSession = $customerSession;
$this->storeManager = $storeManager;
$this->mappingHelper = $mappingHelper;
$this->searchContext = $searchContext;
$this->categoryRepository = $categoryRepository;
}

/**
Expand Down Expand Up @@ -144,6 +161,13 @@ public function afterGetFilters(
}

$result = $filters;

if ($containerConfiguration->getName() === 'catalog_view_container') {
$this->updateSearchContext(
$containerConfiguration->getStoreId(),
$this->getCurrentCategoryId($containerConfiguration, $searchCriteria)
);
}
}

return $result;
Expand Down Expand Up @@ -198,6 +222,24 @@ private function getCurrentCategoryId(ContainerConfigurationInterface $container
return $categoryId;
}

/**
* Update the search context using current store id and category Id.
*
* @param integer $storeId Store id.
* @param integer $categoryId Category Id.
*
* @return void
*/
private function updateSearchContext($storeId, $categoryId)
{
$this->searchContext->setStoreId($storeId);
$category = $this->categoryRepository->get($categoryId, $storeId);

if ($category->getId()) {
$this->searchContext->setCurrentCategory($category);
}
}

/**
* Indicates if the plugin should be used or not.
*
Expand Down

0 comments on commit 31aa0f0

Please sign in to comment.