Skip to content
Advertisement

Magento get the SKU of the first simple product in configurable

I need to get the SKU for a simple product that is part of a configurable in Magento.

Something along the lines of:

if($_product is simple) {
    echo $_product->getSku();
}
elseif($_product is configurable) {
    $simples = $_product-> //Get simple products;

    foreach($simples as $simple) {
        echo $simple->getSku();
    }
}

Also how can I check for the product type?

Thanks.

Advertisement

Answer

Magento 1

Did not tested but should work:

    $product = Mage::getModel('catalog/product')->load(1);
    if ($product->isSimple()) {
         echo $product->getSku(); 
    } else {
        $childProducts = Mage::getModel('catalog/product_type_configurable')
                    ->getUsedProducts(null,$product);
        foreach ($childProducts as $childProduct) {
           echo $childProduct->getSku();
        }
    }

You can use:

if ($product->getTypeId() == "configurable") 
{
    //Your code here
}

or

if ($product->isConfigurable()) 
{
    //Your code here
}

To check type of product.

Magento 2

/** @var MagentoCatalogModelProduct $configurableProduct */

$simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);

foreach ($simpleProducts as $product) {
    echo "Product Sku: " . $product->getSKU();
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement