I am using magento 2.1.8 Enterprise edition. I do not want to save search terms which have zero search results. Is there any solution ?
There is no option in configuration to stop saving the search terms with 0 search results. Please help.
I am using magento 2.1.8 Enterprise edition. I do not want to save search terms which have zero search results. Is there any solution ?
There is no option in configuration to stop saving the search terms with 0 search results. Please help.
Try this.
Create a custom module and create following files.
File: etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Search\Model\Query">
<plugin name="save_greater_then_zero_search_results" type="[Vendor]\[Module]\Plugin\Query" />
</type>
</config>
File: Plugin/Query.php
<?php
namespace [Vendor]\[Module]\Plugin;
class Query
{
public function aroundSaveIncrementalPopularity($subject, $callable)
{
return $subject;
}
public function aroundSaveNumResults($subject, $callable, $numResults)
{
if($numResults > 0)
{
$subject->getResource()->saveIncrementalPopularity($subject);
$subject->setNumResults($numResults);
$subject->getResource()->saveNumResults($subject);
}
return $subject;
}
}
Replace [Vendor] and [Module] with your actual vendor and module name. Hope this helps.