3

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 !

  • Your code is correct I've used same snippet to create configurable product, worth to try with is_percent => false – Mufaddal Dec 22 '14 at 12:11
  • Have you tried using SCP? It sounds like what you are trying to achieve: magentocommerce.com/magento-connect/… – B00MER Dec 23 '14 at 18:42
  • @B00MER No, we do not use SCP because of specific needs. – lenybernard Dec 24 '14 at 13:48
  • @Mufaddal replacing 0 by false did not changed anything. – lenybernard Dec 24 '14 at 13:49
  • But I wish to update the name of the configurable product using a script , how do I update the name of the config product – Jaisa May 13 '18 at 17:18
5

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. :)

0

Try dumping your $_product->getPrice() to check if the correct price is returned or just try changing this code to:

Mage::getModel('catalog/product')->load($_product->getId())->getPrice()

and see if it helps.

If this won't help, try putting Mage::app()->setCurrentStore(0); in front of your methods' logic, to set your store to admin. The other way is to load your products with store set. E.g.:

$simpleProducts = Mage::getModel('catalog/product')->setStore(0)
                      ->getCollection()
                      // your filters go there, don't forget to add 'price' to select
                      ->load();

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.