Skip to content

Commit

Permalink
Merge pull request #86 from Nosto/bug/fix-web-filters
Browse files Browse the repository at this point in the history
ADS-2847: Fix filters for toolbar plugin
  • Loading branch information
olsi-qose authored Nov 17, 2020
2 parents e75f866 + 2eddd09 commit 9bcc1bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 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.

## 3.1.0-rc4
* Fix issue where filters were not passed when using MySQL

## 3.1.0-rc3
* Return Magento sorting when CM call fails

Expand Down
16 changes: 14 additions & 2 deletions Model/Filter/WebFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace Nosto\Cmp\Model\Filter;

use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
use Magento\Framework\Exception\LocalizedException;
use Magento\Store\Model\Store;
use Nosto\NostoException;
Expand Down Expand Up @@ -113,6 +114,7 @@ public function mapIncludeFilter(Item $item)
return;
}

/** @var Attribute $attributeModel */
$attributeModel = $filter->getData('attribute_model');
if ($attributeModel === null) {
return;
Expand All @@ -124,7 +126,6 @@ public function mapIncludeFilter(Item $item)
return;
}

$filterName = $item->getName();
$value = '';
switch ($frontendInput) {
case 'price':
Expand All @@ -150,7 +151,18 @@ public function mapIncludeFilter(Item $item)
return;
}
try {
$this->mapValueToFilter($filterName, $value);
$attributeCode = $attributeModel->getAttributeCode();
if (!is_string($attributeCode)) {
$this->logger->debugCmp(
sprintf(
'Cannot build include filter for "%s" attribute ',
$attributeModel->getName()
),
$this
);
return;
}
$this->mapValueToFilter($attributeCode, $value);
} catch (NostoException $e) {
$this->logger->exception($e);
}
Expand Down
33 changes: 31 additions & 2 deletions Plugin/Catalog/Block/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
use Magento\Framework\DB\Select;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\LayeredNavigation\Block\Navigation\State;
use Magento\Store\Model\Store;
use Nosto\Cmp\Helper\Data as NostoCmpHelperData;
use Nosto\Cmp\Helper\SearchEngine;
use Nosto\Cmp\Model\Filter\WebFilters;
use Nosto\Cmp\Logger\LoggerInterface;
use Nosto\Cmp\Model\Filter\WebFilters;
use Nosto\Cmp\Model\Service\Recommendation\StateAwareCategoryService;
use Nosto\Cmp\Utils\CategoryMerchandising as CategoryMerchandisingUtil;
use Nosto\Helper\ArrayHelper as NostoHelperArray;
Expand All @@ -64,6 +65,9 @@ class Toolbar extends AbstractBlock
/** @var WebFilters */
private $filters;

/** @var State */
private $state;

private static $isProcessed = false;

/**
Expand All @@ -76,6 +80,7 @@ class Toolbar extends AbstractBlock
* @param LoggerInterface $logger
* @param SearchEngine $searchEngineHelper
* @param WebFilters $filters
* @param State $state
*/
public function __construct(
Context $context,
Expand All @@ -85,10 +90,12 @@ public function __construct(
ParameterResolverInterface $parameterResolver,
LoggerInterface $logger,
SearchEngine $searchEngineHelper,
WebFilters $filters
WebFilters $filters,
State $state
) {
$this->searchEngineHelper = $searchEngineHelper;
$this->filters = $filters;
$this->state = $state;
parent::__construct(
$context,
$parameterResolver,
Expand Down Expand Up @@ -131,6 +138,8 @@ public function afterSetCollection(// phpcs:ignore EcgM2.Plugins.Plugin.PluginWa
"Collection is not instanceof ProductCollection"
);
}
//@phan-suppress-next-line PhanTypeMismatchArgument
$this->buildFilters($store);
$result = $this->getCmpResult(
$this->getCurrentPageNumber()-1,
$subjectCollection->getPageSize()
Expand Down Expand Up @@ -176,6 +185,26 @@ private function getCmpResult($start, $limit)
);
}

/**
* @param Store $store
* @return WebFilters
*/
private function buildFilters(Store $store)
{
// Build filters
//@phan-suppress-next-next-line PhanTypeMismatchArgument
/** @noinspection PhpParamsInspection */
$this->filters->init($store);
try {
$this->filters->buildFromSelectedFilters(
$this->state->getActiveFilters()
);
} catch (LocalizedException $e) {
$this->getLogger()->exception($e);
}
return $this->filters;
}

/**
* @param ProductCollection $collection
* @param array $nostoProductIds
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": "3.1.0-rc3",
"version": "3.1.0-rc4",
"require-dev": {
"magento-ecg/coding-standard": "3.*",
"magento/module-store": "101.0.4",
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="3.1.0-rc3">
<module name="Nosto_Cmp" setup_version="3.1.0-rc4">
<sequence>
<module name="Nosto_Tagging"/>
</sequence>
Expand Down

0 comments on commit 9bcc1bb

Please sign in to comment.