Francis Lewis
Francis Lewis

Reputation: 8980

Configurable product asking for a super attribute

I'm programmatically creating a configurable product in Magento, and I have everything working except for when I browse to the product, it asks me to select a configurable attribute for the product.

I can't seem to find code that will add the configurable attribute. My code is below:

$parentProduct->setAttributeSetId(4)
    ->setTypeId('configurable')
    ->setStockData(array(
        'manage_stock'  => 0,
        'is_in_stock'   => 1,
        'qty'           => 99999,
        'min_sale_qty'  => 0,
        'max_sale_qty'  => 0,
    ))
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setTaxClassId(12) // P0000000
    ->setCreatedAt(time())
    ->setName($row['name'])
    ->setSku($sku)
    ->setPrice($row['price'])
    ->setCategoryIds(array(2, 3))
    ->setStatus(1)
    ->setWeight(1)
    ->setWebsiteIDs(array(1))
    ->setDescription($row['description'])
    ->setShortDescription($row['short_desc'])
    ->setHasOptions(true)
    ->setRequiredOptions(true);

$parentProduct->save();

Upvotes: 1

Views: 1018

Answers (1)

Francis Lewis
Francis Lewis

Reputation: 8980

Found what I was looking for finally.

$parentProduct->setUsedProductAttributeIds(array(80)); // where 80 is color

$data = array(
    '0' => array(
        'id'        => NULL,
        'label'     => 'Color',
        'position'  => NULL,
        'values'    => array(
            '0' => array(
                'value_index'   => 4,
                'label'         => 'Silver',
                'is_percent'    => 0,
                'pricing_value' => '0',
                'attribute_id'  =>'80',
            ),
            '1' => array(
                'value_index'   => 3,
                'label'         => 'Gold',
                'is_percent'    => 0,
                'pricing_value' => '0',
                'attribute_id'  =>'80',
            ),
        ),
        'attribute_id'      => 80,
        'attribute_code'    => 'color',
        'frontend_label'    => 'Color',
        'html_id'           => 'config_super_product__attribute_0',
    ),
);
$parentProduct->setConfigurableAttributesData($data);

Now when I visit the product, it no longer asks me to specify a configurable attribute.

Upvotes: 3

Related Questions