Instead of showing "Your search returned no results." I would like to give the user some links to other pages in the site. When I look in module-catalog-search/view/frontend/layout I see three files but they apply to all search results. Is there an easy way to do this with a layout or template file or do I have to build a module?
2 Answers
There are 2 templates being used for result, one for simple search ( using top search field ) and other is for advance search field :
For Top search field result :
module_catalog-search/view/frontend/templates/result.phtml
For Advance Search result :
module_catalog-search/view/frontend/templates/advanced/result.phtml
And both search fields search data / record within product catalog, override the template(s), and add your condition in that template to show whatever data you want to show if there is no record is returned.
search for $block->getResultCount()
and use this for condition.
-
Can I just make changes to the template or do I need to make a module like this- magento.stackexchange.com/questions/211635/…– tjjenCommented Mar 8, 2019 at 16:28
-
@tjjen, in your case i don't recommend developing module, you just need to add pages link to cms pages. so just add static cms pages links in case $block-getResultCount() < 1 or empty. Commented Mar 8, 2019 at 17:00
You have to redirect from "Your search returned no results." to "other page". You can override result.phtml to your theme.
And setRedirect link as you want to redirect appropriate page. I have redirect to contact page when result page is "Your search returned no results." You can change as you want.
File path: magento/app/design/frontend/Vendor/Theme/Magento_CatalogSearch/templates/result.phtml
<?php
<?php if ($block->getResultCount()): ?>
<?= $block->getChildHtml('tagged_product_list_rss_link') ?>
<div class="search results">
<?php if ($messages = $block->getNoteMessages()):?>
<div class="message notice">
<div>
<?php foreach ($messages as $message):?>
<?= /* @escapeNotVerified */ $message ?><br />
<?php endforeach;?>
</div>
</div>
<?php endif; ?>
<?= $block->getProductListHtml() ?>
</div>
<?php else: ?>
<div class="message notice">
<div>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$redirect = $objectManager->get('\Magento\Framework\App\Response\Http');
$redirect->setRedirect($this->getUrl('contact'));
?>
</div>
</div>
<?php endif; ?>
Let me know if any query.
Hope it help!