Skip to content

Commit

Permalink
Merge branch '2.8.x' into 2.9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Aug 3, 2020
2 parents cb25f1f + d133038 commit 207c5d9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function addCategoryData($data)

$documentSource = $category->getDocumentSource();

$title = $documentSource['name'];
$title = $documentSource['name'] ?? '';
if (is_array($title)) {
$title = current($title);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public function addData($storeId, array $indexData)
$price = $priceModifier->getPrice($priceDataRow);

$indexData[$productId]['price'][] = [
'price' => $price,
'original_price' => $originalPrice,
'price' => (float) $price,
'original_price' => (float) $originalPrice,
'is_discount' => $price < $originalPrice,
'customer_group_id' => (int) $priceDataRow['customer_group_id'],
'tax_class_id' => (int) $priceDataRow['tax_class_id'],
'final_price' => $priceDataRow['final_price'],
'min_price' => $priceDataRow['min_price'],
'max_price' => $priceDataRow['max_price'],
'final_price' => (float) $priceDataRow['final_price'],
'min_price' => (float) $priceDataRow['min_price'],
'max_price' => (float) $priceDataRow['max_price'],
];

if (!isset($indexData[$productId]['indexed_attributes'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Aurelien FOUCRET <[email protected]>
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
Expand Down Expand Up @@ -445,6 +446,23 @@ function (\Magento\Framework\Api\Search\Document $doc) {
return parent::_renderFiltersBefore();
}

/**
* Set _pageSize false since it is managed by the engine and might have been changed since _renderFiltersBefore.
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*
* {@inheritDoc}
*/
protected function _beforeLoad()
{
if ($this->_pageSize !== false) {
$this->originalPageSize = $this->_pageSize;
$this->_pageSize = false;
}

return parent::_beforeLoad();
}

/**
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Smile\ElasticsuiteCore\Model\Search\RequestMapper;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
* Extenstion of the category form UI data provider.
Expand All @@ -40,14 +42,31 @@ class RequestMapperPlugin
*/
private $categoryRepository;

/**
* @var \Smile\ElasticsuiteVirtualCategory\Model\Category\Filter\Provider
*/
private $filterProvider;

/**
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory
*/
private $queryFactory;

/**
* Constructor.
*
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository Category repository.
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository Category repository.
* @param \Smile\ElasticsuiteVirtualCategory\Model\Category\Filter\Provider $filterProvider Category Filter provider.
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory.
*/
public function __construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository)
{
public function __construct(
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
\Smile\ElasticsuiteVirtualCategory\Model\Category\Filter\Provider $filterProvider,
QueryFactory $queryFactory
) {
$this->categoryRepository = $categoryRepository;
$this->queryFactory = $queryFactory;
$this->filterProvider = $filterProvider;
}

/**
Expand All @@ -69,11 +88,9 @@ public function afterGetFilters(
SearchCriteriaInterface $searchCriteria
) {
if ($this->isEnabled($containerConfiguration) && isset($result['category.category_id'])) {
$categoryId = current(array_values($result['category.category_id']));
$storeId = $containerConfiguration->getStoreId();
$storeId = $containerConfiguration->getStoreId();
$result[] = $this->getCategoriesQuery($result['category.category_id'], $storeId);

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

Expand All @@ -91,4 +108,54 @@ private function isEnabled(ContainerConfigurationInterface $containerConfigurati
{
return in_array($containerConfiguration->getName(), $this->productSearchContainers);
}

/**
* Get search query for a given category Id
*
* @SuppressWarnings(PHPMD.ElseExpression)
*
* @param array $categoriesFilter The category filters
* @param int $storeId The store Id
*
* @return QueryInterface|null
*/
private function getCategoriesQuery($categoriesFilter, $storeId)
{
$result = [];

foreach ($categoriesFilter as $operator => $categoryIds) {
if (!is_array($categoryIds)) {
$categoryIds = [$categoryIds];
}

$queries = [];
foreach ($categoryIds as $categoryId) {
$queries[] = $this->getCategorySubQuery($categoryId, $storeId);
}

if ($operator === 'in') {
$result[] = $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['should' => $queries]);
} else {
$result += $queries;
}
}

return $this->queryFactory->create(QueryInterface::TYPE_BOOL, ['must' => $result]);
}

/**
* Get search query for a given category Id
*
* @param int $categoryId The category Id
* @param int $storeId The store Id
*
* @return QueryInterface|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getCategorySubQuery($categoryId, $storeId)
{
$category = $this->categoryRepository->get($categoryId, $storeId);

return $this->filterProvider->getQueryFilter($category);
}
}

0 comments on commit 207c5d9

Please sign in to comment.