4

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

1
  • Can you please upload your code ? Dec 12, 2018 at 10:34

3 Answers 3

3

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

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

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

To get Search Result count :

$resultCount = $block->getResultCount();

To get Search Keyword :

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

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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