1

I'm trying to create a configure product with multiple attributes. Here is my code.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $simpleProduct = Mage::getModel('catalog/product');
    try {
        $simpleProduct
        //    ->setStoreId(1) //you can set data in store scope
            ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
            ->setAttributeSetId(20) //ID of a attribute set named 'default'
            ->setTypeId('simple') //product type
            ->setCreatedAt(strtotime('now')) //product creation time
        //    ->setUpdatedAt(strtotime('now')) //product update time
            ->setSku('simple99y'.time()) //SKU
            ->setName('test simple product99') //product name
            ->setWeight(4.0000)
            ->setStatus(1) //product status (1 - enabled, 2 - disabled)
            ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
            ->setManufacturer(28) //manufacturer id
            ->setColor(24)
            ->setNewsFromDate('06/26/2014') //product set as new from
            ->setNewsToDate('06/30/2014') //product set as new to
            ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
            ->setPrice(11.22) //price in form 11.22
            ->setCost(22.33) //price in form 11.22
            ->setSpecialPrice(00.44) //special price in form 11.22
            ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
            ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
            ->setMsrpEnabled(1) //enable MAP
            ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
            ->setMsrp(99.99) //Manufacturer's Suggested Retail Price
            ->setMetaTitle('test meta title 2')
            ->setMetaKeyword('test meta keyword 2')
            ->setMetaDescription('test meta description 2')
            ->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
                    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                    'is_in_stock' => 1, //Stock Availability
                    'qty' => 999 //qty
                )
            )
            ->setCategoryIds(array(3, 10)); //assign product to categories
        $simpleProduct->save();

    } catch (Exception $e) {
        Mage::log($e->getMessage());
        error_log( print_r($e->getMessage(),true) );
    }

    $simpleProduct2 = Mage::getModel('catalog/product');
    try {
        $simpleProduct2
        //    ->setStoreId(1) //you can set data in store scope
            ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
            ->setAttributeSetId(20) //ID of a attribute set named 'default'
            ->setTypeId('simple') //product type
            ->setCreatedAt(strtotime('now')) //product creation time
        //    ->setUpdatedAt(strtotime('now')) //product update time
            ->setSku('simple98w'.time()) //SKU
            ->setName('test simple product98') //product name
            ->setWeight(4.0000)
            ->setStatus(1) //product status (1 - enabled, 2 - disabled)
            ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
            ->setManufacturer(28) //manufacturer id
            ->setColor(25)
            ->setNewsFromDate('06/26/2014') //product set as new from
            ->setNewsToDate('06/30/2014') //product set as new to
            ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
            ->setPrice(22.22) //price in form 11.22
            ->setCost(33.33) //price in form 11.22
            ->setSpecialPrice(00.44) //special price in form 11.22
            ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
            ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
            ->setMsrpEnabled(1) //enable MAP
            ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
            ->setMsrp(99.99) //Manufacturer's Suggested Retail Price
            ->setMetaTitle('test meta title 2')
            ->setMetaKeyword('test meta keyword 2')
            ->setMetaDescription('test meta description 2')
            ->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
                    'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
                    'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
                    'is_in_stock' => 1, //Stock Availability
                    'qty' => 999 //qty
                )
            )
            ->setCategoryIds(array(3, 10)); //assign product to categories
        $simpleProduct2->save();

    } catch (Exception $e) {
        Mage::log($e->getMessage());
        error_log( print_r($e->getMessage(),true) );
    }

    $configProduct = Mage::getModel('catalog/product');
    try {
    $configProduct
    //    ->setStoreId(1) //you can set data in store scope
            ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
            ->setAttributeSetId(20) //ID of a attribute set named 'default'
            ->setTypeId('configurable') //product type
            ->setCreatedAt(strtotime('now')) //product creation time
    //    ->setUpdatedAt(strtotime('now')) //product update time
            ->setSku('configurable96'.time()) //SKU
            ->setName('test config product96') //product name
            ->setWeight(4.0000)
            ->setStatus(1) //product status (1 - enabled, 2 - disabled)
            ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
            ->setManufacturer(28) //manufacturer id
            ->setNewsFromDate('06/26/2014') //product set as new from
            ->setNewsToDate('06/30/2014') //product set as new to
            ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
            ->setPrice(11.22) //price in form 11.22
            ->setCost(22.33) //price in form 11.22
            ->setSpecialPrice(00.44) //special price in form 11.22
            ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
            ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
            ->setMsrpEnabled(1) //enable MAP
            ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
            ->setMsrp(99.99) //Manufacturer's Suggested Retail Price
            ->setMetaTitle('test meta title 2')
            ->setMetaKeyword('test meta keyword 2')
            ->setMetaDescription('test meta description 2')
            ->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, 10)) //assign product to categories
        ;
        /**/
        /** assigning associated product to configurable */
        /**/
        $configProduct->getTypeInstance()->setUsedProductAttributeIds(array(92, 211)); //attribute ID of attribute 'color' and 'Banda' in my store

        $configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
        error_log("config attributes: " . print_r($configurableAttributesData, true));
        $configProduct->setCanSaveConfigurableAttributes(true);
        $configProduct->setConfigurableAttributesData($configurableAttributesData);

        $configurableProductsData = array();
        $configurableProductsData[$simpleProduct->getId()] = array( //['920'] = id of a simple product associated with this configurable
            '0' => array(
                'label' => 'Green', //attribute label
                'attribute_id' => '92', //attribute ID of attribute 'color' in my store
                'value_index' => '24', //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '21' //value for the pricing
             ),
            '1' => array(
                'label' => 'Johny', //attribute label
                'attribute_id' => '211', //attribute ID of attribute 'color' in my store
                'value_index' => '238', //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '4' //value for the pricing
            )
        );


        $configurableProductsData[$simpleProduct2->getId()] = array( //['920'] = id of a simple product associated with this configurable
            '0' => array(
                'label' => 'Grey', //attribute label
                'attribute_id' => '92', //attribute ID of attribute 'color' in my store
                'value_index' => '25', //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '25' //value for the pricing
            ),
            '1' => array(
                'label' => 'Johny', //attribute label
                'attribute_id' => '211', //attribute ID of attribute 'color' in my store
                'value_index' => '238', //value of 'Green' index of the attribute 'color'
                'is_percent' => '0', //fixed/percent price for this option
                'pricing_value' => '4' //value for the pricing
            )
        );


        $configProduct->setConfigurableProductsData($configurableProductsData);
        $configProduct->save();
        ini_set('memory_limit', '1024M');

        $configProduct = null;
        $product_id = $message['product_id'];
        $configProduct = Mage::getModel('catalog/product');
        $configProduct->load( $product_id );

        //$stat = file_put_contents("/home/lalani/Desktop/logfile.log", json_encode($configProduct->getData()));

        error_log("success " . $stat);
    } catch (Exception $e) {
        Mage::log($e->getMessage());
        error_log( print_r($e->getMessage(),true) );
    }

and in the result I got this enter image description here

This shouldn't be like this. There should be two options with Color and one with Banda. However, I have created an configurable product with single attribute but I am not getting success with two attributes. Please help me to figure out the problem.

Updated

Below link shows the admin side of 'Banda' attribute enter image description here

1
  • Did you find what was wrong? I have the exact same issue.
    – Xyz
    Jan 28, 2016 at 17:07

1 Answer 1

0

I've faced the same problem. The problem is Banda attribute didn't set to simple products(Apply to*). Solution, you need to set configurable and simple product also. So, You've to check once again Banda attribute.

5
  • thanks for your reply. Sorry I didn't get your point. can u tell me that where should i change the code. because I'm following this link inchoo.net/magento/… can u give me the solution via relating it with this example ??
    – johnyBaba
    Dec 23, 2015 at 10:39
  • you've to change back-end only not code admin->catalog->manage attributes->edit banda attribute -> set apply to simple products
    – Shiva
    Dec 23, 2015 at 12:32
  • I've updated my question with snapshot of "admin-side of 'Banda' attribute".....Please check and guide me further please
    – johnyBaba
    Dec 23, 2015 at 13:29
  • Check once again color attribute if you create that attribute. The problem is, configurable product creates many simple product which is apply by attributes, That's why products are not showing down in associated products section. then, Re-index all in index management and clear cache.. that's it.
    – Shiva
    Dec 24, 2015 at 7:38
  • And you can check once comments of http://inchoo.net/magento/programmatically-create-a-configurable-magento-product/ tutorial.
    – Shiva
    Dec 24, 2015 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.