2

How to get Search result count and keyword in result.phtml in magento2.

I have overriden the Magento_CatalogSearch/templates/result.phtml file.

I need to show the count returned from the search result as well as the key searched here.

Is that possible to get it. Please anyone suggest on this.

Thanks

  • Can you please upload your code ? – Rohan Hapani Dec 12 '18 at 10:34
2

You can get Search result count $block->getResultCount()

and use keyword for get $this->helper('Magento\CatalogSearch\Helper\Data')->getEscapedQueryText()

  • hi @Amit, got count but keyword is not returned – jafar pinjar Dec 12 '18 at 10:39
  • My mistake, i don't paste proper class name – Amit Bera Dec 12 '18 at 10:42
  • ok, thanks it worked, could you check the Terms and condition issue? – jafar pinjar Dec 12 '18 at 10:48
2

To get Search Result count :

$resultCount = $block->getResultCount();

To get Search Keyword :

$helper = $this->helper('Magento\Search\Helper\Data');
$searchKeyword = $helper->getEscapedQueryText();
1

If you use Magento\CatalogSearch\Block\Result this block in your custom module. Then you can use this below code :

For search keyword :

$this->catalogSearchData->getEscapedQueryText()

OR

$this->helper('Magento\CatalogSearch\Helper\Data')->getEscapedQueryText()

For count :

$block->getResultCount()

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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