Wanted to change from two columns to the one column xml when catalog search results returns zero counts. Using Magento 2.3 version of the community. Showing empty sidebar on the left side.
1 Answer
I don't think there's a simple way to achieve your aim. Here's the logic Magento follows when you search for a product:
First, when you type your query text into the search form and hit enter, this triggers Magento_Search to go to its Helper file and get the URL for the form's action. This URL is 'catalogsearch/result.' We don't yet know how many search results we'll get.
\Magento\Search\view\frontend\templates\form.mini.phtml
<form class="form minisearch" id="search_mini_form" action="<?= /* @escapeNotVerified */ $helper->getResultUrl() ?>" method="get">
\Magento\Search\Helper\Data.php
public function getResultUrl($query = null) { return $this->_getUrl( 'catalogsearch/result', ['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()] ); }
Magento resolves 'catalogsearch/result' into Magento_CatalogSearch's Result/Index controller, and runs its execute() function. We still don't know how many search results we'll get.
\Magento\CatalogSearch\Controller\Result\Index.php
public function execute() {...}
Magento grabs our controller's layout (catalogsearch_result_index.xml) and starts rendering containers and blocks. Once again, we don't know how many search results we'll get.
\Magento\CatalogSearch\view\frontend\layout\catalogsearch_result_index.xml
<block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml" cacheable="false">
Within our layout file, the result.phtml template is tied to the Result.php block. Once this block is rendered, we finally know how many search results we've got.
\Magento\CatalogSearch\view\frontend\templates\result.phtml
<?php if ($block->getResultCount()): ?>
\Magento\CatalogSearch\Block\Result.php
public function getResultCount() {...}
From this, you can probably see the difficulty. We don't know how many search results we're going to get until Magento has already chosen the layout and is in the middle of rendering the search results page.
To achieve your goal, you would probably need to override the controller in a di.xml file, query for search results within your overridden execute function, and then redirect to a new controller that renders a new template from a new layout of your own design if the query count is zero.
I briefly and unsuccessfully tried to query for search results from within the controller, but it can probably be done with some time and elbow grease. However, you'd then be querying for search results twice (once in the background and once during page render), with a consequent hit to performance; not to mention overriding a significant file to achieve what seems to me a trivial aesthetic (although I suppose "trivial" might be up to the client, not the developer).
Unless you've got a client who absolutely demands this change, I'd personally stick with vanilla Magento here. For me, it wouldn't be worth the time, effort, and non-trivial structural changes for such a minor benefit.