Magento – Creating configurable products programmatically – pricing_value not saved

ce-1.9.0.1configurable-productprice

I have a custom xml file and I make a massive import of the products. I have some simples products under configurables ones. All is working well, configurable products are created with the simple products the "associated products" tab but one last thing still doesn't work : the price of each product.

Actually, Each simple product has its own price and the correct value is well saved in its attribute but in the "super product attribute configuration" panel, the values are empty.

Super product attributes configuration

When I fill the price fields manually, it works but it obviously must be done by the script, programmatically.

Here is my function to create the configurable product :

protected function createConfigurableProductFromSimpleProduct($product, $flagshipID)
{
    $configurableProduct = $product->duplicate();
    $configurableProduct->getResource()->save($configurableProduct);
    $configurableProduct= Mage::getModel('catalog/product')->load($configurableProduct->getId());

    $configurableProduct->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
                     ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                     ->setSku($flagshipID)
                     ->setDefRef($product_id)
                     ->getTypeInstance()->setUsedProductAttributeIds(array($this->getAttributeId('root_colors'))); //attribute ID of attribute 'root_colors' in my store

    $configurableProduct->setName($configurableProduct->getName());
    $configurableProduct->setStatus(1);
    $configurableProduct->setStockData(array(
        'is_in_stock' => 1
    ));
    $configurableProduct->setUrlKey($configurableProduct->getName());
    $configurableProduct->save();

    return $configurableProduct;
}

And here is the code for linking simple products to this configurable product :

protected function linkSimpleProductsToConfigurableProduct($simpleProducts, $configurableProduct)
{
    $configurableProductsData = array();
    foreach ($simpleProducts as $_product) {
        $_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
        $_product->getResource()->save($_product);
        $configurableProductsData[$_product->getId()] = array( //[id] = id of a simple product associated with this configurable
            '0' => array(
                'label'         => $this->getAttributeRawValue($this->getAttributeId('root_colors'), $_product->getRoot()), //attribute label
                'attribute_id'  => $this->getAttributeId('root_colors'), //attribute ID of discriminator attribute in my store
                'value_index'   => $_product->getColor(),
                'pricing_value' => $_product->getPrice(),
                'is_percent'    => '0'
            )
        );
    }
    $configurableAttributesData = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray();
    $configurableProduct->setCanSaveConfigurableAttributes(true);
    $configurableProduct->setConfigurableAttributesData($configurableAttributesData);
    $configurableProduct->setConfigurableProductsData($configurableProductsData);
    var_dump('saving configurable product after having link some simple products');
    $configurableProduct->save();
}

Any help is welcome, thank you !

Best Answer

I've been playing with this for a few hours and here is the result. Please note that it's slightly different from your case in sense that I created simple products and configurable product manually from admin. Configurable product was left empty (no associated products), required attribute was Color.

Here is the code:

    $configurable = Mage::getModel('catalog/product')->load(892);

    $simpleProducts = Mage::getResourceModel('catalog/product_collection')
        ->addIdFilter(array(237, 241, 243, 246))
        ->addAttributeToSelect('color')
        ->addAttributeToSelect('price');

    $configurableProductsData = array();
    $configurableAttributesData = $configurable->getTypeInstance()->getConfigurableAttributesAsArray();

    foreach ($simpleProducts as $simple) {
        $productData = array(
            'label'         => $simple->getAttributeText('color'),
            'attribute_id'  => $this->_getAttributeId('color'),
            'value_index'   => (int) $simple->getColor(),
            'is_percent'    => 0,
            'pricing_value' => $simple->getPrice()
        );

        $configurableProductsData[$simple->getId()] = $productData;
        $configurableAttributesData[0]['values'][] = $productData;
    }

    $configurable->setConfigurableProductsData($configurableProductsData);
    $configurable->setConfigurableAttributesData($configurableAttributesData);
    $configurable->setCanSaveConfigurableAttributes(true);
    Mage::log($configurableProductsData, null, 'configurableProductsData.log', true);
    Mage::log($configurableAttributesData, null, 'configurableAttributesData.log', true);
    $configurable->save();

And here is the data dump of $configurableProductsData and $configurableAttributesData variables:

$configurableProductsData

(
[237] => Array
    (
        [label] => Charcoal
        [attribute_id] => 92
        [value_index] => 17
        [is_percent] => 0
        [pricing_value] => 160.0000
    )

[241] => Array
    (
        [label] => Silver
        [attribute_id] => 92
        [value_index] => 15
        [is_percent] => 0
        [pricing_value] => 510.0000
    )

[243] => Array
    (
        [label] => White
        [attribute_id] => 92
        [value_index] => 22
        [is_percent] => 0
        [pricing_value] => 455.0000
    )

[246] => Array
    (
        [label] => Blue
        [attribute_id] => 92
        [value_index] => 27
        [is_percent] => 0
        [pricing_value] => 490.0000
    )
)

$configurableAttributesData

(
[0] => Array
    (
        [id] => 202
        [label] => Color
        [use_default] => 0
        [position] => 0
        [values] => Array
            (
                [0] => Array
                    (
                        [label] => Charcoal
                        [attribute_id] => 92
                        [value_index] => 17
                        [is_percent] => 0
                        [pricing_value] => 160.0000
                    )

                [1] => Array
                    (
                        [label] => Silver
                        [attribute_id] => 92
                        [value_index] => 15
                        [is_percent] => 0
                        [pricing_value] => 510.0000
                    )

                [2] => Array
                    (
                        [label] => White
                        [attribute_id] => 92
                        [value_index] => 22
                        [is_percent] => 0
                        [pricing_value] => 455.0000
                    )

                [3] => Array
                    (
                        [label] => Blue
                        [attribute_id] => 92
                        [value_index] => 27
                        [is_percent] => 0
                        [pricing_value] => 490.0000
                    )

            )

        [attribute_id] => 92
        [attribute_code] => color
        [frontend_label] => Color
        [store_label] => Color
    )
)

I hope this helps you solve your issue. :)

Related Topic