Magento: programmatic search depending on store

Magento: programmatic search depending on store

October 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: programmatic search depending on store

Magento: Programmatic Search Depending on Store

Searching across multiple stores in Magento can be quite complex, especially when you want to retrieve data programmatically. Let’s dive into your issue of searching “test” in two different stores and combining the results.

Understanding the Current Setup

Firstly, let’s clarify what’s happening:

  • You have two stores.
  • When searching “test” in Store 1, you get 5 results.
  • When searching “test” in Store 2, you get 3 results.
  • Currently, both searches are independent of each other.

The Challenge: Combining Search Results

You want to display the total number of results from both stores when a search is performed in Store 1. This requires fetching data from two different contexts and merging them.

Why Your Current Attempt Failed

You attempted to change the store context before running the search:

Mage::app()->setCurrentStore($secondStoreId);

However, Magento’s session management is quite strict. Changing the current store context programmatically like this doesn’t always work because of the way Magento’s multi-store functionality is implemented.

The Correct Approach: Multiple Queries

To achieve your goal, you’ll need to run two separate searches—one for Store 1 and one for Store 2—and then sum their results. Here’s a step-by-step guide:

Step 1: Set Up the Search Helper

Ensure you have the catalogsearch module enabled in both stores.

php bin/magento module:enable Magento_CatalogSearch

Then, make sure the search is enabled for each store under Stores > Configuration > Catalog > Catalog Search.

Step 2: Create a Custom Block and Template

Create a new block and template file to display the combined results. This will help you keep the logic separate from your existing template files.

For instance, create a new block called Namespace_ModuleName/Block/CombinedResults.php:

class CombinedResults extends \Magento\Framework\View\Element\Template
{
    public function getTotalResults()
    {
        $search1 = Mage::helper('catalogSearch')->getQuery();
        $search1->setStoreId(1);
        $results1 = count($search1->getResults());

        $search2 = Mage::helper('catalogSearch')->getQuery();
        $search2->setStoreId(2);
        $results2 = count($search2->getResults());

        return $results1 + $results2;
    }
}

And a corresponding template file under app/design/frontend/Namespace/ModuleName/templates/combinedresults.phtml:

<?php echo $block->getTotalResults(); ?>

Step 3: Display the Combined Results

In your existing search result template, include this new block:

{{block class="Namespace_ModuleName\Block\CombinedResults" template="combinedresults.phtml"}}

Conclusion and Further Reading

Programmatic searches in Magento often require a deep understanding of the core modules involved. By running separate queries for each store and using our custom block to combine them, you can achieve your goal.

Remember that Magento’s multi-store functionality is complex because it maintains separate database entries for each store. Always ensure you’re working with the correct context when performing operations across multiple stores.

For more advanced search functionality in your Magento setup, consider exploring third-party extensions or custom development solutions. Check out Laravel Company for expert Magento consultancy and development services to handle complex integrations like this one.

Happy coding!

Catalog Extensions Errors CLI

Our network