Reputation: 493
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.
Upvotes: 2
Views: 2151
Reputation: 787
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 Magento\Catalog\Model\Product $configurableProduct */
$simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
foreach ($simpleProducts as $product) {
echo "Product Sku: " . $product->getSKU();
}
Upvotes: 3