0

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.

1
  • Anyone else have solution for this? I used the below module , it is not saving search terms with 0 search results but it increments the uses in admin by 2 . we are also using mirasvit catalog search extension. After disabling the module, the default functionality increments the uses by 1 only.
    – chanchal
    Mar 1, 2019 at 7:51

1 Answer 1

2

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.

11
  • Thankyou. It works.
    – chanchal
    Feb 26, 2019 at 4:25
  • there is a issue, after using this code the number of uses in search terms under marketing is increasing by 2. Please help.
    – chanchal
    Feb 28, 2019 at 5:59
  • The line $subject->getResource()->saveIncrementalPopularity($subject); is saving the uses with an increment of 2. whereas It should save with One incremented value.
    – chanchal
    Feb 28, 2019 at 6:45
  • checking for it. Will get back to you in sometime.
    – Yash Shah
    Feb 28, 2019 at 7:16
  • Not sure, why this is happening this way, i checked with commenting my code and it still auto increament by 2 ...
    – Yash Shah
    Feb 28, 2019 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.