Tuesday, May 10, 2022

[FIXED] How to get a list of skus in magento

Issue

How to get a list of skus whiout using the looping in magento. Example: I am using below code with my conditons.

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

Now I want to result as array('A001','A002'....) etc. I don't want to iterate (loop) the product collection.

Please suggest.


Solution

If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.

$skus = array();
foreach ($productsCollection as $product) {
    $skus[] = $product->getSku();
}

If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.

$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');


Answered By - Marius
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.