Need to get Simple Products with Configurable Option from Configurable Product
-
12-12-2019 - |
Question
I need to get details of all simple products associated with Configurable product along with product attributes
I used the existing magento functionality which returns empty value.
function getConfig($productIds) {
$configurableData = array();
$configProdCollctn = Mage::getResourceModel('catalog/product_collection');
$configProdCollctn->addAttributeToFilter('entity_id',array('in'=> $productIds))
->addAttributeToFilter('type_id',
array('eq'=> Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE));
while ($product = $configProdCollctn->fetchItem()) {
$prodAttrOptions = $product->getTypeInstance(true)->getConfigurableOptions($product);
foreach ($prodAttrOptions as $prodAttrOption) {
$configurableData[$product->getId()] = array();
foreach ($prodAttrOption as $optionValues) {
$val = $optionValues['option_title'];
$configurableData[$product->getId()][] = array(
$optionValues['sku'] => $optionValues['attribute_code']."=".$val
);
}
}
}
return ($configurableData);
}
This returns empty array.
for eg if config prod (A) has option of color and size. i need simple product A1,A2,A3 with color and size associated with options A1 = array(color => red,size => M) A2 = array(color => red,size => S)
Kindly Let me know how can I achieve this
Solution
I got the solution to the above requirment..
$product= Mage::getModel('catalog/product')->load($configId);
#Check if the product has children
if ($product->type_id == 'configurable') {
$options = array();
// Get any super_attribute settings we need
$productAttributesOptions = $product->getTypeInstance(true)->getConfigurableOptions($product);
foreach ($productAttributesOptions as $productAttributeOption) {
$options[$product->getId()] = array();
foreach ($productAttributeOption as $optionValues) {
$val = $this->trimValue($optionValues['option_title']);
$options[$product->getId()][] = array (
$optionValues['sku'] => $optionValues['attribute_code']."=".$val
) ;
}
}
OTHER TIPS
Take a look at Getting attribute from simple products of configurable products
$productId = 1; //config product id
$product = Mage::getModel('catalog/product')->load($productId);
$configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
$simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($simpleCollection as $simple){
//$simple->getName();
}
See Get simple products belonging to configurable products in magento
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange