Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Created December 30, 2013 17:13
Show Gist options
  • Select an option

  • Save tegansnyder/8184936 to your computer and use it in GitHub Desktop.

Select an option

Save tegansnyder/8184936 to your computer and use it in GitHub Desktop.
Get a list of skus in a category ID comma separated in Magento
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('default');
$cat_id = 2117;
$skus = array();
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = $category->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
foreach ($collection as $product) {
$product_id = $product->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$skus[] = $_product->getSku();
}
$sku_list = implode(',', $skus);
echo $sku_list;
@alvinnguyen

Copy link
Copy Markdown

You should not run product model load inside a loop like this as it would cause performance issue.

Not sure if the SKU is already available inside the loop in your example without loading the model.

If not, try

 $collection = Mage::getModel('catalog/category')
                    ->load($categoryId)
                    ->getProductCollection();
// Run the foreach loop, the SKU should be available by using $product->getSku()

@AH72KING

Copy link
Copy Markdown

Can you write the same code for magneto 2 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment