I need to override the search term
-
15-04-2021 - |
Question
I need override \vendor\magento\module-catalog-search\Model\Layer\Search\Plugin\CollectionFilter.php File, i need get the search term and replace "-" and "." for nothing. Which a best method for make this? I know how to override view files in my design theme but i don't know how to override Model files.
Example:
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Model\Layer\Search\Plugin;
use Magento\Catalog\Model\Category;
use Magento\Search\Model\QueryFactory;
/**
* Catalog search plugin for search collection filter in layered navigation.
*/
class CollectionFilter
{
/**
* @var \Magento\Search\Model\QueryFactory
*/
protected $queryFactory;
/**
* @param QueryFactory $queryFactory
*/
public function __construct(QueryFactory $queryFactory)
{
$this->queryFactory = $queryFactory;
}
/**
* Add search filter criteria to search collection
*
* @param \Magento\Catalog\Model\Layer\Search\CollectionFilter $subject
* @param null $result
* @param \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $collection
* @param Category $category
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterFilter(
\Magento\Catalog\Model\Layer\Search\CollectionFilter $subject,
$result,
$collection,
Category $category
) {
/** @var \Magento\Search\Model\Query $query */
$query = $this->queryFactory->get();
if (!$query->isQueryTextShort()) {
$text = $query->getQueryText();
$text = str_replace(array("-","."),"",$text);
$collection->addSearchFilter($text);
}
}
}
Solution
I think the best way to do this is to edit this before it gets to the model and the collection. My suggestion is to create an observer on the controller_action_predispatch_catalogsearch_result_index
event:
in your module, in etc/frontend/events.xml
file, add:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_catalogsearch_result_index">
<observer name="change_query_text" instance="Vendor\Module\Observer\ChangeQueryText"/>
</event>
</config>
in your observer class Vendor\Module\Observer\ChangeQueryText.php
, you can do the following:
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
/**
* Class ChangeQueryText
* @package Vendor\Module\Observer
*/
class ChangeQueryText implements ObserverInterface
{
private $request;
/**
* @param RequestInterface $request
*/
public function __construct(
RequestInterface $request
) {
$this->request = $request;
}
/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$queryString = trim($this->request->getQuery('q'));
$newQuery = preg_replace('/[\-\.]/', '', $queryString);
$this->request->setParam('q', $newQuery);
}
}
Code is not tested but should work properly in theory :)