Skip to content

Commit

Permalink
Merge pull request #163 from Nosto/release/5.1.0
Browse files Browse the repository at this point in the history
Release/5.1.0
  • Loading branch information
dairbuirabass authored Oct 24, 2022
2 parents 5478911 + b1e6b74 commit 577eceb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

### 5.1.0
* Add support for Magento 2 GrahpQl filters

### 5.0.1
* Fix parsing existing query params for product URL generation

Expand Down
106 changes: 99 additions & 7 deletions Model/Service/Facet/BuildGraphQlFacetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,122 @@

namespace Nosto\Cmp\Model\Service\Facet;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Nosto\Cmp\Exception\FacetValueException;
use Nosto\Cmp\Model\Facet\Facet;
use Nosto\Cmp\Utils\Traits\LoggerTrait;
use Nosto\Operation\Recommendation\ExcludeFilters;
use Nosto\Operation\Recommendation\IncludeFilters;
use Nosto\Tagging\Helper\Data as NostoDataHelper;
use Nosto\Tagging\Logger\Logger;

class BuildGraphQlFacetService
{
const CATEGORY_FILTER = 'category_filter';

const VISIBILITY_FILTER = 'visibility_filter';

const PRICE_FILTER = 'price_filter';

use LoggerTrait {
LoggerTrait::__construct as loggerTraitConstruct; // @codingStandardsIgnoreLine
}

/** @var NostoDataHelper */
private NostoDataHelper $nostoDataHelper;

/** @var ProductAttributeRepositoryInterface */
private ProductAttributeRepositoryInterface $productAttributeRepository;

/**
* BuildGraphQlFacetService constructor.
* @param NostoDataHelper $nostoDataHelper
* @param ProductAttributeRepositoryInterface $productAttributeRepository
* @param Logger $logger
*/
public function __construct(
NostoDataHelper $nostoDataHelper,
ProductAttributeRepositoryInterface $productAttributeRepository,
Logger $logger
) {
$this->loggerTraitConstruct(
$logger
);
$this->nostoDataHelper = $nostoDataHelper;
$this->productAttributeRepository = $productAttributeRepository;
}

/**
* @param Store $store
* @param array $requestData
* @return Facet
*/
public function getFacets(array $requestData): Facet
public function getFacets(Store $store, array $requestData): Facet
{
$includeFilters = new IncludeFilters();
$excludeFilters = new ExcludeFilters();

if (isset($requestData['filters']['price_filter'])) {
$priceFilters = $requestData['filters']['price_filter'];
$includeFilters->setPrice(
isset($priceFilters['from']) ? $priceFilters['from'] : null,
isset($priceFilters['to']) ? $priceFilters['to'] : null
);
foreach ($requestData['filters'] as $filter) {
if ($filter['name'] === self::CATEGORY_FILTER || // Skip visibility and category filters
$filter['name'] === self::VISIBILITY_FILTER
) {
continue;
} elseif ($filter['name'] === self::PRICE_FILTER) { // Price filters
$includeFilters->setPrice(
isset($filter['from']) ? $filter['from'] : null,
isset($filter['to']) ? $filter['to'] : null
);
} else { // Custom field filters
try {
$attributeCode = $filter['field'];
$filterValues = $filter['value'];
$attribute = $this->productAttributeRepository->get($attributeCode);
$values = [];

if (is_string($filterValues)) { // eq attribute
$values = [$this->getOptionText($store, $attribute, $filterValues)];
} else { // in attribute
foreach ($filterValues as $value) {
$values[] = $this->getOptionText($store, $attribute, $value);
}
}

if ($attributeCode === $this->nostoDataHelper->getBrandAttribute($store)) {
$includeFilters->setBrands($values);
} else {
$includeFilters->setCustomFields(
$attributeCode,
$values
);
}
} catch (NoSuchEntityException | FacetValueException $e) {
$this->logger->exception($e);
}
}
}

return new Facet($includeFilters, $excludeFilters);
}

/**
* @param Store $store
* @param ProductAttributeInterface $attribute
* @param mixed $value
* @return string|boolean
* @throws FacetValueException
*/
private function getOptionText($store, $attribute, $value)
{
/** @phan-suppress-next-next-line PhanUndeclaredMethod */
/** @noinspection PhpUndefinedMethodInspection */
$result = $attribute->getSource()->getOptionText($value);
if ($result) {
return $result;
}

throw new FacetValueException($store, $attribute->getAttributeCode(), $value);
}
}
2 changes: 1 addition & 1 deletion Plugin/Framework/Search/Request/GraphQlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function parseLimit(Store $store, array $requestData)
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
public function getFilters(Store $store, array $requestData)
{
return $this->buildFacetService->getFacets($requestData);
return $this->buildFacetService->getFacets($store, $requestData);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nosto/module-nostocmp",
"description": "Nosto Category Merchandising extension for Magento 2",
"type": "magento2-module",
"version": "5.0.1",
"version": "5.1.0",
"require-dev": {
"magento-ecg/coding-standard": "3.*",
"magento/module-store": "101.1.2",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<!--suppress XmlUnboundNsPrefix -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Nosto_Cmp" setup_version="5.0.1">
<module name="Nosto_Cmp" setup_version="5.1.0">
<sequence>
<module name="Nosto_Tagging"/>
</sequence>
Expand Down

0 comments on commit 577eceb

Please sign in to comment.