Search Product by name in magento and get array of product ids

Search Product by name in magento and get array of product ids

July 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Search Product by name in magento and get array of product ids

Searching Products by Name in Magento: A Comprehensive Guide

Searching for products programmatically is a common requirement in e-commerce platforms like Magento. If you’re looking to find all product IDs whose names contain a specific search string, this guide will walk you through the process.

Understanding the Requirement

The goal is to create a function getProductIdsBySearch($searchstring, $storeId) that accepts a search string and a store ID as parameters. This function should return an array of product IDs where the product name contains the given search string.

Let’s first look at the sample data:

IDProduct Name
1Temp
2ProductTemp
3ProductTempData
4ABCTEMPXYZ
5ABCXYZ
6Tempdata

If we pass temp as the search string, the function should return an array containing IDs [1, 2, 3, 4, 6]. The ID 5 is excluded because the name “ABCXYZ” does not contain the search term.

The Magento Approach

Magento uses the Zend framework internally for its database operations. To fetch product data, we can use the Mage::getModel('catalog/product')->getCollection() method to get a collection of all products in the specified store.

Here’s a basic example:

$collection = Mage::getModel('catalog/product')->getCollection();

However, this returns all products regardless of the search string. To filter by name, we need to use ->addFieldToFilter() method with "name" as the field and the % operator for a partial match.

Implementing the Function

Now let’s write the function:

function getProductIdsBySearch($searchstring, $storeId) {
    $ids = array();

    // Get all products in the specified store
    $collection = Mage::getModel('catalog/product')
        ->addFieldToFilter(
            'name',
            array('like' => '%'.$searchstring.'%')     // Filter by name containing search string
        )
        ->addStoreFilter($storeId)                    // Filter by specific store ID
    ;

    foreach ($collection as $product) {
        $ids[] = $product->getId();                   // Add product ID to the array
    }

    return $ids;
}

This function first creates a collection of products that match the search string in their names. It then filters this collection for only those products assigned to the specified store.

The foreach loop iterates over each product in the filtered collection and adds its ID to the $ids array. Finally, it returns this array containing all matching product IDs.

Testing the Function

To test this function, you can call it with a search string and the desired store ID:

$searchString = 'temp';
$storeId = 1; // Replace with your store ID

$productIds = getProductIdsBySearch($searchString, $storeId);

print_r($productIds);

This should output an array of product IDs matching the search string in the specified store.

Conclusion

Searching for products by name programmatically in Magento involves using the Mage::getModel('catalog/product')->getCollection() method to fetch all products, then filtering this collection based on the search criteria. The ->addFieldToFilter() method is key to achieving a partial match with the % operator.

Remember that you need to be logged into your Magento admin interface and have the necessary permissions to execute such code snippets.

For more advanced product searching or if you’re looking to optimize performance for larger catalogs, consider using indexes and considering the Zend_Db_Select class. You can find detailed information on these topics in our Magento Developer Guide at Laravel Company.

Happy coding!

Catalog Database Themes

Our network