Magento – How to create a configurable product with associated products

magento-1magento-enterprise

The following code does create a configurable product, however, when I open the product in the backend, the following message appears:

when I create the configurable product and open it in the backend, I get this message: Select Configurable Attributes Only attributes with scope "Global", input type "Dropdown" and Use To Create Configurable Product "Yes" are available.

Material 
Size 
Cotton 
Color 
Colour Group 

I selected size and color, and clicked continue, however, there are no associated products. All of the other product data is there.

//Mage Product
$mpr = Mage::getModel('catalog/product');
$mpr
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
    ->setTaxClassId(5)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    ->setWebsiteIds(array(1))
    ->setAttributeSetId(4) // You can determine this another way if you need to.
    ->setSku("C12345")
    ->setName("C12345")
    ->setQty(25)
    ->setShortDescription('short description')
    ->setDescription('description')
    ->setPrice(1)
    ->setStockData(array(
        'use_config_manage_stock' => 1,
        'is_in_stock' => 1,
        'is_salable' => 1,
    ));

    $productData = array(
         '7039604' =>
            array('0' => array('attribute_id' => '85', 'label' => 'ROYAL','value_index' => '28563', 'is_percent' => 0, 'pricing_value' => '')
                 ,'1' => array('attribute_id' => '192', 'label' => '14', 'value_index' => '28728', 'is_percent' => 0, 'pricing_value' => '')
            )
    );
    $attributeData = array(
        '0' => array(
            'id' => NULL
            ,'label' => 'Color'
            ,'position' => NULL
            ,'values' => array(
                '0' => array('value_index' => 28563, 'label' => 'ROYAL', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '85')
            )
            ,'attribute_id' => 85
            ,'attribute_code' => 'color'
            ,'frontend_label' => 'Color'
            ,'html_id' => 'config_super_product__attribute_0')
        ,'1' => array(
            'id' => NULL
            ,'label' => 'Rivers Size'
            ,'position' => NULL
            ,'values' => array(
                '0' => array('value_index' => 28728, 'label' => '14', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '192')
            )
            ,'attribute_id' => 192
            ,'attribute_code' => 'rivers_size'
            ,'frontend_label' => 'Rivers Size'
            ,'html_id' => 'config_super_product__attribute_1')
    );
$mpr->setConfigurableProductsData($productData);
$mpr->setConfigurableAttributesData($attributeData);
$mpr->setCanSaveConfigurableAttributes(true);
$mpr->save();

Best Answer

Looking at your code, you are missing the part where you inform Magento which attributes are used in your configurable (this would correspond to the first step when creating a configurable product in the admin, where you pick the used attributes). Put this before the call to setConfigurableProductsData:

$mpr->getTypeInstance()->setUsedProductAttributeIds(array(85,192));
Related Topic