Appearance
Get All simple product from a Configurable Product in Magento Product View
Note: This guide covers Magento 1. Magento 2 uses a different architecture (Dependency Injection and ProductRepository) for product retrieval.
In Magento, a configurable product is made up of multiple simple products, each with different options such as size or color. To get all the simple products associated with a configurable product in the product view, you can use the following code snippet:
Example of getting all the simple products associated with a configurable product in Magento Product View
php
<?php
// Replace with your configurable product ID
$configurableProductId = 123;
$product = Mage::getModel('catalog/product')->load($configurableProductId);
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
foreach ($childProducts as $child) {
// do something with each simple product
}This code first loads the configurable product by its ID, then uses the getUsedProducts method to get an array of all the simple products associated with it. You can then loop through the array and access the details of each simple product, such as its ID, name, price, etc.
You can also use the following methods to get the details of simple product from configurable product
Example of getting the details of simple product from configurable product in Magento
php
<?php
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
foreach ($productAttributeOptions as $productAttribute) {
foreach ($productAttribute['values'] as $attribute) {
// Note: This approach iterates through attribute options and may be less efficient than direct child product retrieval
$simpleProduct = Mage::getModel('catalog/product')->load($attribute['product_id']);
}
}This code snippet uses the getTypeInstance method to get the attribute options of the configurable product, and then iterates through the options to retrieve the details of each simple product.