1

I need to create configurable product using simple product collection programatically.

Create configurable product code

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect(array('name'))->load();
    $productName = array();

    foreach($collection as $collection1) {
        $productName[] = $collection1->getName();
    }

    $getData = array_unique($productName);

    $productCollection = Mage::getModel('catalog/product')->getCollection()
            ->addFieldToFilter('name', array('in'=> $getData));
    $productCollection->load();

    $newArr = array();
    foreach ($productCollection->getData() as $val) {
        $newArr[$val[name]] = $val;    
    }
    $productUniqeArray = array_values($newArr);

    foreach ($productUniqeArray as $productData) {

        /* define attribute code */
        $attributeColorCode = 'color';

        /* return function to getting the attribute color id */
        $attribute_details = Mage::getSingleton("eav/config")->getAttribute('catalog_product', $attributeColorCode);
        $attribute = $attribute_details->getData();
        $attribute_colorid = $attribute['attribute_id'];

        $_product = Mage::getModel('catalog/product')->load($productData['entity_id']);
        //echo "<pre>";
        //print_r($_product);
        echo $_product->getName(); //product name
        $configProduct = Mage::getModel('catalog/product');

        $configProduct
        ->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())) //website ID the product is assigned to, as an array
        ->setTypeId('configurable') //product type
        ->setCreatedAt(strtotime('now')) //product creation time
        ->setSku($_product->getName().'_config') //SKU
        ->setName($_product->getName()) //product name
        ->setWeight($_product->getWeight())
        ->setStatus(1) //product status (1 - enabled, 2 - disabled)
        ->setTaxClassId(2) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
        ->setPrice(11.22) //price in form 11.22
        ->setDescription('This is a long description')
        ->setShortDescription('This is a short description')
        ->setMediaGallery(array('images' => array(), 'values' => array())) //media gallery initialization
        ->setStockData(array(
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock' => 1, //manage stock
                'is_in_stock' => 1, //Stock Availability
            )
        )
        ->setCategoryIds(array(3)) //assign product to categories
        ;

        $simpleProducts = Mage::getResourceModel('catalog/product_collection')
        ->addIdFilter(array(2,3))
        ->addAttributeToSelect('color')
        ->addAttributeToSelect('price')
        ->addFieldToFilter('type_id','simple')
        ->addFieldToFilter('name',$productData['name']);



        $configProduct->setCanSaveConfigurableAttributes(true);
        $configProduct->setCanSaveCustomOptions(true);

        //$attribute_colorid = 92;
        $configProduct->getTypeInstance()->setUsedProductAttributeIds(array($attribute_colorid)); //attribute ID of attribute 'color' in my store
        $configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
        $configProduct->setCanSaveConfigurableAttributes(true);
        $configProduct->setConfigurableAttributesData($configurableAttributesData);
        $configurableProductsData = array();

        foreach ($simpleProducts as $simple) {
            //Mage::log($simple->getData(), null, 'simple.log', true);
            $productData = array(
            'label' => $simple->getAttributeText('color'),
            'attribute_id' => $attribute_colorid,
            'value_index' => (int) $simple->getColor(),
            'is_percent' => 0,
            'pricing_value' => $simple->getPrice()
            );

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

        }

        $configProduct->setConfigurableProductsData($configurableProductsData);

        $configProduct->setConfigurableAttributesData($configurableAttributesData);
        $configProduct->setCanSaveConfigurableAttributes(true);



        Mage::log($configurableProductsData, null, 'configurableProductsData.log', true);
        Mage::log($configurableAttributesData, null, 'configurableAttributesData.log', true);

        $configProduct->save();

    }

Getting error : Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (m1937.catalog_product_entity, CONSTRAINT FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID FOREIGN KEY (attribute_set_id)

Your Answer

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

Browse other questions tagged or ask your own question.