1

I have the following script which uses Magento models to create or update a configurable product.

// Method to create or update configurable product with configurable options
function createOrUpdateConfigurableProduct($product_data, $configurable_products_data)
{
    // Init product
    $product = Mage::getModel('catalog/product');

    // Load product id by sky
    $product_id = Mage::getModel('catalog/product')
        ->getIdBySku($product_data['sku']);

    // If product already exists
    if ($product_id)
    {
        // Load product
        $product->load($product_id);
    }

    // Set product data
    $product
        ->setTypeId('configurable')
        ->setAttributeSetId(4)
        ->setSku($product_data['sku'])
        ->setName($product_data['name'])
        ->setDescription($product_data['description'])
        ->setShortDescription($product_data['description'])
        ->setMetaTitle($product_data['name'])
        ->setMetaDescription($product_data['description'])
        ->setPrice($product_data['price'])
        ->setTaxClassId(2)
        ->setWebsiteIDs(array(1))
        ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->setStockData(array(
            'use_config_manage_stock' => 0,
            'manage_stock'            => 1,
            'is_in_stock'             => ($product_data['qty'] >= 1 ? 1 : 0)
        ));

    // Workout all the attribute ids used
    $used_attribute_ids = array();
    foreach ($configurable_products_data as $product_id => $options) {
        foreach ($options as $option) {
            if (!in_array($option['attribute_id'], $used_attribute_ids)) {
                $used_attribute_ids[] = $option['attribute_id'];
            }
        }
    }

    // Set configurable attributes data
    $product->getTypeInstance()->setUsedProductAttributeIds($used_attribute_ids);
    $configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();
    $product->setCanSaveConfigurableAttributes(true);
    $product->setConfigurableAttributesData($configurableAttributesData);

    // Set configurable products data & save
    $product->setConfigurableProductsData($configurable_products_data);
    $product->save();
}

Where $configurable_products_data looks something like this:

Array
(
    [49] => Array
        (
            [0] => Array
                (
                    [label] => 27 in.
                    [attribute_id] => 155
                    [value_index] => 47
                    [is_percent] => 0
                    [pricing_value] => 210
                )

            [1] => Array
                (
                    [label] => 32L
                    [attribute_id] => 156
                    [value_index] => 50
                    [is_percent] => 0
                    [pricing_value] => 210
                )

        )

    [50] => Array
        (
            [0] => Array
                (
                    [label] => 28 in.
                    [attribute_id] => 155
                    [value_index] => 48
                    [is_percent] => 0
                    [pricing_value] => 210
                )

            [1] => Array
                (
                    [label] => 32L
                    [attribute_id] => 156
                    [value_index] => 50
                    [is_percent] => 0
                    [pricing_value] => 210
                )

        )

    [51] => Array
        (
            [0] => Array
                (
                    [label] => 28 in.
                    [attribute_id] => 155
                    [value_index] => 48
                    [is_percent] => 0
                    [pricing_value] => 210
                )

            [1] => Array
                (
                    [label] => 34L
                    [attribute_id] => 156
                    [value_index] => 51
                    [is_percent] => 0
                    [pricing_value] => 210
                )

        )

)

When this code runs, on first run - it work's fine and create the configurable product. However, on the second run, it doesn't seem to be able to update existing configurable product, it's throwing the following cryptic error message:

Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '52-155' for key 'UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID''

Any ideas?

0

i use this code to insert and update configurable product in my custom extension.

where all data get in data variable

$pid is configurable product id.
$aid is attribute id.
before you use this code u set session variable is edit to 0 or 1. 0- if new product , 1- if edit existing product

$data = $this->getRequest ()->getPost();
            $pid=Mage::getSingleton('core/session')->getAttributeId();
            $aid=Mage::getSingleton('core/session')->getAttributeValue();
            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $prd = Mage::getModel('catalog/product')->load($pid);
            try
            {
                $totalRow=count($data['product_id']);
                if($totalRow>0)
                {
                         $configurable = Mage::getModel('catalog/product')->load($pid);
                         if(!Mage::getSingleton('core/session')->getIsEdit(0))
                         {
                            $configurable->getTypeInstance()->setUsedProductAttributeIds(array($aid));
                         }
                         $configurable->save();
                         $productModel = Mage::getModel('catalog/product');
                         $configAttributeCode = $productModel->getResource()->getAttribute($aid)->getAttributeCode();
                         $simpleProducts = Mage::getResourceModel('catalog/product_collection')
                                ->addIdFilter($data['product_id'])
                                ->addAttributeToSelect($configAttributeCode);

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

                            for($l=0;$l<$totalRow;$l++)
                            {
                                $_product = Mage::getModel('catalog/product')->load($data['product_id'][$l]);
                                $_product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
                                $_product->getResource()->save($_product);
                                $productModel = Mage::getModel('catalog/product');
                                $attr = $productModel->getResource()->getAttribute($aid);
                                $att_label = $attr->getSource()->getOptionText($data['attribute_type'][$l]);
                                $productData = array( 
                                        'label' => $att_label, //attribute label
                                        'attribute_id' => $aid, //attribute ID 
                                        'value_index' => $data['attribute_type'][$l],
                                        'is_percent' => '0', //fixed/percent price for this option
                                        'pricing_value' => $data['product_price'][$l] //value for the pricing
                                );
                                $configurableProductsData[$_product->getId()] = $productData;
                                $configurableAttributesData[0]['values'][$l] = $productData;
                            }

                            $configurable->setConfigurableProductsData($configurableProductsData);
                            $configurable->setConfigurableAttributesData($configurableAttributesData);
                            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
                            $configurable->setCanSaveConfigurableAttributes(true);
                            $configurable->setStatus(1);
                            $configurable->save();

                }
            }
            catch(Exception $e)
            {
                echo $e->getMessage();  
            }

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.