How to get a list of skus in magento

How to get a list of skus in magento

August 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get a list of skus in magento

Retrieving SKUs in Magento Without Looping: A Comprehensive Guide

When working with Magento products, you might find yourself needing to fetch a list of SKUs without having to iterate through the product collection using loops. This guide will walk you through an efficient way to achieve this using Magento’s built-in functionality.

Understanding Your Current Approach

First, let’s break down your current code:

$productsCollection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('sku');

This code retrieves a collection of all products and selects only the sku attribute. However, to get this as an array without looping, we need to adjust our approach.

Extracting SKUs Without Looping

To fetch an array of SKUs directly from the product collection without looping, you can use the getColumnValues() method provided by Magento’s collection:

$skuArray = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('sku')
    ->getColumnValues('sku');

Here’s what we’re doing:

  1. We start by getting the product collection just like before.
  2. We add the sku attribute to the select using addAttributeToSelect('sku').
  3. Then, instead of iterating over the collection (which you mentioned you don’t want to do), we use the getColumnValues() method and pass in 'sku' as the argument.

This method returns an array containing all the SKU values directly from the database without needing to loop through the collection.

Ensuring Your Conditions Are Met

You mentioned that your code has certain conditions. To ensure these are met, you can add filters to your product collection before calling getColumnValues(). For example:

$skuArray = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('sku')
    ->addFieldToFilter('status', array('eq' => 1)) // Only enabled products
    ->getColumnValues('sku');

In this example, we’ve added a filter to include only products with status = 1 (enabled). You can replace this condition with any of the filters Magento supports.

When to Use This Approach

This method is particularly useful when you need a static list of SKUs for:

  • Displaying in a menu or navigation.
  • Comparing against another array.
  • Checking if an item exists before performing other operations.

It’s important to note that this approach directly fetches data from the database, so it should be used judiciously. It works well when you’re looking for a small number of SKUs and don’t need any additional product information.

Conclusion

Fetching SKUs without looping in Magento is straightforward once you know how to use the getColumnValues() method on the product collection. By applying this approach, you can efficiently retrieve an array of SKUs that meet your specific criteria.

Remember, while direct database access like this can be fast, it’s essential to consider the performance implications if you’re dealing with a very large number of products or complex filter conditions.

For more advanced Magento tutorials and best practices, I recommend checking out our blog at Laravel Company. We regularly publish articles on Magento development, optimization, and extension development that can help you better understand the platform’s capabilities.

Happy coding!

Catalog Database

Our network