Skip to content

Commit

Permalink
Integration of search context (Smile-SA#781)
Browse files Browse the repository at this point in the history
* Integration of search context and refactoring the way visibility and stock filter are applied.
  • Loading branch information
romainruaud authored and Rasmus Frederiksen committed Apr 19, 2018
1 parent 780ece1 commit 37b4c3b
Show file tree
Hide file tree
Showing 19 changed files with 742 additions and 16 deletions.
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
);
}
}
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;
}
}
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 src/module-elasticsuite-catalog/Plugin/Layer/CategoryPlugin.php
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;
}
}
4 changes: 0 additions & 4 deletions src/module-elasticsuite-catalog/Plugin/LayerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ public function beforePrepareProductCollection(
\Magento\Catalog\Model\Layer $layer,
\Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection $collection
) {
if ($this->_isEnabledShowOutOfStock() === false) {
$collection->addIsInStockFilter();
}

$this->setSortParams($layer, $collection);
}

Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,27 @@
<requests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Smile_ElasticsuiteCore:etc/elasticsuite_search_request.xsd">

<request name="quick_search_container" label="Catalog Product Search" index="catalog_product" type="product" fulltext="true" />
<request name="quick_search_container" label="Catalog Product Search" index="catalog_product" type="product" fulltext="true">
<filters>
<filter name="stock">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\Stock</filter>
<filter name="visible-in-search">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\VisibleInSearch</filter>
</filters>
</request>

<request name="catalog_product_autocomplete" label="Catalog Product Autocomplete" index="catalog_product" type="product" fulltext="true" />
<request name="catalog_product_autocomplete" label="Catalog Product Autocomplete" index="catalog_product" type="product" fulltext="true">
<filters>
<filter name="stock">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\Stock</filter>
<filter name="visible-in-catalog">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\VisibleInSearch</filter>
</filters>
</request>

<request name="catalog_view_container" label="Category Product View" index="catalog_product" type="product" fulltext="false" />
<request name="catalog_view_container" label="Category Product View" index="catalog_product" type="product" fulltext="false">
<filters>
<filter name="stock">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\Stock</filter>
<filter name="visible-in-catalog">Smile\ElasticsuiteCatalog\Model\Product\Search\Request\Container\Filter\VisibleInCatalog</filter>
</filters>
</request>

<request name="category_search_container" label="Catalog Category Search" index="catalog_category" type="category" fulltext="true"/>

<request name="category_search_container" label="Catalog Category Search" index="catalog_category" type="category" fulltext="true" />
</requests>
9 changes: 9 additions & 0 deletions src/module-elasticsuite-catalog/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,13 @@
<argument name="attributeHelper" xsi:type="object">Smile\ElasticsuiteCatalog\Helper\ProductAttribute</argument>
</arguments>
</type>

<!-- Search Context init -->
<type name="Magento\Catalog\Model\Layer\Category">
<plugin name="initSearchContext" type="Smile\ElasticsuiteCatalog\Plugin\Layer\CategoryPlugin"/>
</type>

<type name="Magento\Search\Model\QueryFactory">
<plugin name="initSearchContext" type="Smile\ElasticsuiteCatalog\Plugin\Search\QueryFactoryPlugin"/>
</type>
</config>
Loading

0 comments on commit 37b4c3b

Please sign in to comment.