forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration of search context (Smile-SA#781)
* Integration of search context and refactoring the way visibility and stock filter are applied.
- Loading branch information
1 parent
780ece1
commit 37b4c3b
Showing
19 changed files
with
742 additions
and
16 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/module-elasticsuite-catalog/Model/Product/Search/Request/Container/Filter/Stock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter; | ||
|
||
use Smile\ElasticsuiteCore\Api\Search\Request\Container\FilterInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Product Stock Default filter. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class Stock implements FilterInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* @var \Magento\Framework\App\Config\ScopeConfigInterface | ||
*/ | ||
private $scopeConfiguration; | ||
|
||
/** | ||
* Visibility filter constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration scope Configuration Interface | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory, | ||
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration | ||
) { | ||
$this->queryFactory = $queryFactory; | ||
$this->scopeConfiguration = $scopeConfiguration; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilterQuery(\Smile\ElasticsuiteCore\Api\Search\ContextInterface $searchContext) | ||
{ | ||
$query = null; | ||
|
||
if (false === $this->isEnabledShowOutOfStock($searchContext->getStoreId())) { | ||
$query = $this->queryFactory->create(QueryInterface::TYPE_TERM, ['field' => 'stock.is_in_stock', 'value' => true]); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* Get config value for 'display out of stock' option | ||
* | ||
* @param int $storeId The Store Id | ||
* | ||
* @return bool | ||
*/ | ||
private function isEnabledShowOutOfStock($storeId = null) | ||
{ | ||
return $this->scopeConfiguration->isSetFlag( | ||
'cataloginventory/options/show_out_of_stock', | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...e-elasticsuite-catalog/Model/Product/Search/Request/Container/Filter/VisibleInCatalog.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter; | ||
|
||
use Smile\ElasticsuiteCore\Api\Search\Request\Container\FilterInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Product Visibility Default filter. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class VisibleInCatalog implements FilterInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* Visibility filter constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory | ||
*/ | ||
public function __construct(\Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory) | ||
{ | ||
$this->queryFactory = $queryFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilterQuery(\Smile\ElasticsuiteCore\Api\Search\ContextInterface $searchContext) | ||
{ | ||
$query = $this->queryFactory->create( | ||
QueryInterface::TYPE_TERMS, | ||
[ | ||
'field' => 'visibility', | ||
'values' => [ | ||
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_CATALOG, | ||
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH, | ||
], | ||
] | ||
); | ||
|
||
return $query; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...le-elasticsuite-catalog/Model/Product/Search/Request/Container/Filter/VisibleInSearch.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter; | ||
|
||
use Smile\ElasticsuiteCore\Api\Search\Request\Container\FilterInterface; | ||
use Smile\ElasticsuiteCore\Search\Request\QueryInterface; | ||
|
||
/** | ||
* Product Visibility Default filter. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class VisibleInSearch implements FilterInterface | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory | ||
*/ | ||
private $queryFactory; | ||
|
||
/** | ||
* Visibility filter constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory Query Factory | ||
*/ | ||
public function __construct(\Smile\ElasticsuiteCore\Search\Request\Query\QueryFactory $queryFactory) | ||
{ | ||
$this->queryFactory = $queryFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFilterQuery(\Smile\ElasticsuiteCore\Api\Search\ContextInterface $searchContext) | ||
{ | ||
$query = $this->queryFactory->create( | ||
QueryInterface::TYPE_TERMS, | ||
[ | ||
'field' => 'visibility', | ||
'values' => [ | ||
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_SEARCH, | ||
\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH, | ||
], | ||
] | ||
); | ||
|
||
return $query; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/module-elasticsuite-catalog/Plugin/Layer/CategoryPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Plugin\Layer; | ||
|
||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
|
||
/** | ||
* Catalog Category Layer Plugin. | ||
* Used to instantiate Search Context when setting current category. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class CategoryPlugin | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Context | ||
*/ | ||
private $searchContext; | ||
|
||
/** | ||
* CategoryPlugin constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Context $searchContext Search Context | ||
*/ | ||
public function __construct(\Smile\ElasticsuiteCore\Search\Context $searchContext) | ||
{ | ||
$this->searchContext = $searchContext; | ||
} | ||
|
||
/** | ||
* Set the current layer category into the Search context after being assigned to the layer. | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* | ||
* @param \Magento\Catalog\Model\Layer\Category $layer The layer | ||
* @param \Magento\Catalog\Api\Data\CategoryInterface $category Current Category | ||
* | ||
* @return \Magento\Catalog\Model\Layer\Category | ||
*/ | ||
public function afterGetCurrentCategory( | ||
\Magento\Catalog\Model\Layer\Category $layer, | ||
\Magento\Catalog\Api\Data\CategoryInterface $category | ||
) { | ||
$this->searchContext->setCurrentCategory($category); | ||
|
||
return $category; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/module-elasticsuite-catalog/Plugin/Search/QueryFactoryPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
* @copyright 2018 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
namespace Smile\ElasticsuiteCatalog\Plugin\Search; | ||
|
||
/** | ||
* Query Factory Plugin. | ||
* Used to init Search Context when query is retrieved. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCatalog | ||
* @author Romain Ruaud <romain.ruaud@smile.fr> | ||
*/ | ||
class QueryFactoryPlugin | ||
{ | ||
/** | ||
* @var \Smile\ElasticsuiteCore\Search\Context | ||
*/ | ||
private $searchContext; | ||
|
||
/** | ||
* CategoryPlugin constructor. | ||
* | ||
* @param \Smile\ElasticsuiteCore\Search\Context $searchContext Search Context | ||
*/ | ||
public function __construct(\Smile\ElasticsuiteCore\Search\Context $searchContext) | ||
{ | ||
$this->searchContext = $searchContext; | ||
} | ||
|
||
/** | ||
* Set the current search query into the Search context after being retrieved. | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* | ||
* @param \Magento\Search\Model\QueryFactory $queryFactory The Query Factory | ||
* @param \Magento\Search\Model\Query $query The Query | ||
* | ||
* @return \Magento\Search\Model\Query | ||
*/ | ||
public function afterGet(\Magento\Search\Model\QueryFactory $queryFactory, \Magento\Search\Model\Query $query) | ||
{ | ||
if ($query && $query->getQueryText() !== '') { | ||
$this->searchContext->setCurrentSearchQuery($query); | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.