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 1


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:

  1. 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.


    <form class="form minisearch" id="search_mini_form" action="<?= /* @escapeNotVerified */ $helper->getResultUrl() ?>" method="get">


    public function getResultUrl($query = null) { return $this->_getUrl( 'catalogsearch/result', ['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()] ); }

  2. 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.


    public function execute() {...}

  3. 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.


    <block class="Magento\CatalogSearch\Block\Result" name="search.result" template="Magento_CatalogSearch::result.phtml" cacheable="false">

  4. 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.


    <?php if ($block->getResultCount()): ?>


    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.

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.