6

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();
3

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));
1

What exactly is your question? You have to relate simple products to the configurable product.

Go into the backend, create simple products. Choose a size and a color and save the product (after adding all the other things).

Then go to your configurable product, choose the tab "Associated products" and add it there.

2
  • I think the question is about how to programatically create a configurable and associate some simple products to it. – Paul Grigoruta Feb 21 '13 at 23:00
  • When I understand him corecct, he expects the simple products to be generated automatically? This is not the case. :-) – Fabian Blechschmidt Feb 21 '13 at 23:02
0

You can try this link to create a configurable product

http://www.codetweet.com/magento/create-configurable-product-magento-programmatically/

From OP's Comment Below: The above link shows how to easily add configurable products in Magento step by step. 1) Create simple products, 2) then bind them to add a configurable product.

3
  • 1
    You should give more details about the contents found at that url. Single link answers are not really appreciated. – Marius Oct 10 '13 at 11:25
  • the above link shows how to easily add configurable procucts in magento step by step.create simple products first then bind them to add a configurable product. – arp Oct 17 '13 at 8:03
  • Thank you Arpit. Single url answers are great when they lead right to the answer. no explanation necessary. – JustinP Dec 5 '13 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.