Magento Get SKU List in Category

I know that Magento categories can be reached with $categories = Mage::getResourceModel('catalog/category_collection') but how can I get a list of these categories and their corresponding product units?

+4
source share
2 answers

If you just need an array containing the category id and skus array ...

 $skuArray = array(); $categoryCollection = Mage::getModel('catalog/category')->getCollection(); foreach ($categoryCollection as $category) { $skuArray[$category->getId()] = $category->getProductCollection()->getColumnValues('sku'); } 

Alternatively, add a new field to each collection object containing skus ...

 $categoryCollection = Mage::getModel('catalog/category')->getCollection(); foreach ($categoryCollection as $category) { $skus = $category->getProductCollection()->getColumnValues('sku'); $category->setData('skus', $skus); } 

This will be an option if you still have to continue working on the collection later in your code, and you still need to access the sku array of the product.

 foreach($categoryCollection as $category) { $categorySkus = $category->getData('skus'); } 
+2
source

You can use:

 $categories = Mage::getResourceModel('catalog/category_collection'); foreach ($categories as $category) { $products = $category->getProductCollection(); foreach ($products as $product) { $sku[$product->getId()] = $product->getSku(); } } 

I save all sku in $sku as an array. You may have different ones.

+2
source

All Articles