Skip to content

Commit

Permalink
Search API : vitual categories support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien FOUCRET committed Jan 22, 2018
1 parent b940892 commit 48fc94b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Smile\ElasticsuiteCore\Model\Search\RequestMapper;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Smile\ElasticsuiteCore\Search\Request\SortOrderInterface;

/**
* Apply catalog product settings to the search API request mapper.
Expand Down Expand Up @@ -95,6 +96,10 @@ public function afterGetSortOrders(
if ($this->isEnabled($containerConfiguration)) {
$sortOrders = [];

if ($containerConfiguration->getName() == "catalog_view_container" && empty($result)) {
$result['position'] = ['direction' => SortOrderInterface::SORT_ASC];
}

foreach ($result as $sortField => $sortParams) {
if ($sortField == 'price') {
$sortParams['nestedFilter'] = ['price.customer_group_id' => $this->customerSession->getCustomerGroupId()];
Expand Down Expand Up @@ -138,7 +143,7 @@ public function afterGetFilters(
$filters[$fieldName] = $filterValue;
}

$result = $filters;
$result = $filters;
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/module-elasticsuite-catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@
</type>

<type name="Smile\ElasticsuiteCore\Model\Search\RequestMapper">
<plugin name="catalogProductRequestMapper" type="Smile\ElasticsuiteCatalog\Plugin\Search\RequestMapperPlugin"/>
<plugin name="catalogProductRequestMapper" type="Smile\ElasticsuiteCatalog\Plugin\Search\RequestMapperPlugin" sortOrder="10"/>
</type>
</config>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer
* versions in the future.
*
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2018 Smile
* @license Open Software License ("OSL") v. 3.0
*/

namespace Smile\ElasticsuiteVirtualCategory\Plugin\Search;

use Smile\ElasticsuiteCore\Model\Search\RequestMapper;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;

/**
* Extenstion of the category form UI data provider.
*
* @category Smile
* @package Smile\ElasticsuiteVirtualCategory
* @author Aurelien FOUCRET <[email protected]>
*/
class RequestMapperPlugin
{
/**
* @var array
*/
private $productSearchContainers = [
'quick_search_container',
'catalog_view_container',
];

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

/**
* Constructor.
*
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository Category repository.
*/
public function __construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}

/**
* Post process catalog filters (virtual categories handling).
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*
* @param RequestMapper $subject Request mapper.
* @param array $result Original filters.
* @param ContainerConfigurationInterface $containerConfiguration Container configuration.
* @param SearchCriteriaInterface $searchCriteria Search criteria.
*
* @return array[]
*/
public function afterGetFilters(
RequestMapper $subject,
$result,
ContainerConfigurationInterface $containerConfiguration,
SearchCriteriaInterface $searchCriteria
) {
if ($this->isEnabled($containerConfiguration) && isset($result['category.category_id'])) {
$categoryId = current(array_values($result['category.category_id']));
$storeId = $containerConfiguration->getStoreId();

$category = $this->categoryRepository->get($categoryId, $storeId);
$result[] = $category->getVirtualRule()->getCategorySearchQuery($category);
unset($result['category.category_id']);
}

return $result;
}

/**
* Indicates if the plugin should be used or not.
*
* @param ContainerConfigurationInterface $containerConfiguration Container configuration.
*
* @return boolean
*/
private function isEnabled(ContainerConfigurationInterface $containerConfiguration)
{
return in_array($containerConfiguration->getName(), $this->productSearchContainers);
}
}
4 changes: 4 additions & 0 deletions src/module-elasticsuite-virtual-category/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@
type="Smile\ElasticsuiteVirtualCategory\Plugin\Catalog\Category\DataProviderPlugin" />
</type>

<type name="Smile\ElasticsuiteCore\Model\Search\RequestMapper">
<plugin name="virtualCategoryProductRequestMapper" type="Smile\ElasticsuiteVirtualCategory\Plugin\Search\RequestMapperPlugin" sortOrder="20"/>
</type>

</config>

0 comments on commit 48fc94b

Please sign in to comment.