How to get a Magento product collection as a comma separated list of SKU's

How to get a Magento product collection as a comma separated list of SKU's

September 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get a Magento product collection as a comma separated list of SKU's

Getting a Magento Product Collection as a Comma Separated List of SKUs

As a senior developer working extensively with Magento, I often receive queries about how to manipulate product data effectively in the admin area. In this post, we’ll explore how you can retrieve a collection of products from a specific category and output them as a comma separated list of SKUs.

Understanding Your Current Approach

Your current implementation relies on retrieving configuration data for featured products:

$configData = Mage::getStoreConfig('featured_products');
$featuredlist = $configData['settings']['featuredlist'];

This approach works well when you need to store a predefined list of product SKUs. However, when dealing with dynamic collections based on category filtering or other criteria, we require a more robust solution.

Retrieving Products from a Category

To achieve this, let’s modify your existing product collection retrieval code:

$_productCollection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array('eq' => 4)) // visibility = enabled
    ->addCategoryFilter($_category)
    ->load();

This code retrieves all enabled products from the specified category using a resource model. The addAttributeToSelect('*') method ensures all product attributes are loaded, while addCategoryFilter($_category) restricts the collection to only those within the given category.

Transforming the Collection into a Comma Separated List

Now that we have our product collection, let’s convert it into a comma separated list of SKUs. We can achieve this by iterating through the collection and using PHP’s built-in implode function:

$skuList = '';
foreach ($_productCollection as $product) {
    if (!empty($skuList)) {
        $skuList .= ',';
    }
    $skuList .= $product->getSku();
}

In this code snippet, we initialize an empty string $skuList and then loop through each product in the collection. For each product, we append its SKU to the list, adding a comma separator unless it’s the first item.

Implementing Your Solution

To use this solution effectively, you’ll need to integrate it into your custom module’s admin configuration or wherever you’re handling category-based product lists. Here’s an example of how you might implement this in a controller:

public function getCategoryProductSkusAction()
{
    $categoryId = (int) $this->getRequest()->getParam('category_id');
    $_category = Mage::getModel('catalog/category')->load($categoryId);

    if (!$_category->getId()) {
        Mage::throwException(Mage::helper('category')->__('This category no longer exists.'));
    }

    // Retrieve and transform the collection
    $productCollection = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('visibility', array('eq' => 4))
        ->addCategoryFilter($_category)
        ->load();

    $skuList = '';
    foreach ($productCollection as $product) {
        if (!empty($skuList)) {
            $skuList .= ',';
        }
        $skuList .= $product->getSku();
    }

    // Output or process the SKU list as required
    Mage::renderPartial('category/sku_list.phtml', array('sku_list' => $skuList));
}

In this example, we first retrieve the category ID from the request parameters. We then load the category model and validate its existence before proceeding to retrieve the product collection.

After transforming the collection into a comma separated SKU list, we render a partial view (category/sku_list.phtml) that can display or process this data as needed.

Conclusion

Retrieving a collection of products from a category in Magento and outputting them as a comma separated list of SKUs is a common requirement for many customizations. By leveraging the power of resource models, attribute filtering, and PHP’s built-in functions, you can achieve this efficiently and effectively.

Remember to always follow best practices when working with Magento collections:

  1. Use resource models instead of collection objects where possible.
  2. Load your collections only when necessary to minimize performance overhead.
  3. Filter your collections based on the minimum required criteria.
  4. Handle potential exceptions gracefully, such as when a category doesn’t exist.

If you found this post helpful and want to learn more about Magento development best practices, I invite you to explore our comprehensive guides at LaravelCompany. We cover everything from architecture overviews to advanced customization techniques.

Catalog API Admin Configuration

Our network