B-219 Sec-55 Noida, India
+918010221733

Creating Configurable product programmatically in magento

<?php
set_time_limit(0);
error_reporting(E_ALL | E_STRICT);
error_reporting(1);
ini_set(‘display_errors’, 1);
define(‘D_S’, DIRECTORY_SEPARATOR);
require_once ‘app/Mage.php’;
umask(0);
Mage::app();
if (function_exists(‘d’) === false) {

    function d($data, $die = 0, $z = 1, $msg = 1) {
        echo”<br/><pre style=’padding:2px 5px;background: none repeat scroll 0 0 #E04E19;clear: both;color: #FFFFFF;float: left;font-family: Times New Roman;font-style: italic;font-weight: bold;text-align: left;’>”;
        if ($z == 1)
            Zend_Debug::dump($data);
        else if ($z == 2)
            var_dump($data);
        else
            print_r($data);
        echo”</pre>”;
        if ($d == 1)
            die();
    }

}

function get_attribute_id($option, $type) {
    $attributeId = Mage::getResourceModel(‘eav/entity_attribute’)->getIdByCode(‘catalog_product’, $type);
    $attribute = Mage::getModel(‘catalog/resource_eav_attribute’)->load($attributeId);
    $attributeOptions = $attribute->getSource()->getAllOptions();

    foreach ($attributeOptions as $opts_arr) {
        if (strtoupper($opts_arr[‘label’]) == strtoupper($option)) {
            return $opts_arr[‘value’];
        }
    }
    return FALSE;
}

$_simple_products = array();

function attributeValueExists($arg_attribute, $arg_value) {
    $attribute_model = Mage::getModel(‘eav/entity_attribute’);
    $attribute_options_model = Mage::getModel(‘eav/entity_attribute_source_table’);

    $attribute_code = $attribute_model->getIdByCode(‘catalog__simple_product’, $arg_attribute);
    $attribute = $attribute_model->load($attribute_code);

    $attribute_table = $attribute_options_model->setAttribute($attribute);
    $options = $attribute_options_model->getAllOptions(false);

    foreach ($options as $option) {
        if ($option[‘label’] == $arg_value) {
            return $option[‘value’];
        }
    }

    return false;
}

//$_product = Mage::getModel(‘catalog/product’)->load(46);
$collection = Mage::getModel(‘catalog/product’)->getCollection();
$collection->addAttributeToSelect(‘*’);
// d($collection->getData()); exit;
$in = 0;
$product_sku_collection = array();
$product_dummy_collection = array();
foreach ($collection->getData() as $val) {

    $product_dummy_collection[$in] = $val;
    $collection = Mage::getModel(‘catalog/product’)->getCollection();
    $collection->addAttributeToFilter(‘confiugrable_sku’, array(‘eq’, $val[‘sku’]));
    if (count($collection->getData()) > 0) {
        $product_sku_collection[‘sku’][$in][‘conf’] = $val[‘sku’];
        $product_sku_collection[‘sku’][$in][‘simpleprodcollection’] = $collection->getData();
        $in++;
    }
}
$color_attribute = Mage::getModel(‘eav/config’)->getAttribute(‘catalog_product’, 92);
foreach ($color_attribute->getSource()->getAllOptions(true, true) as $option) {
    $attributeArray[$option[‘value’]] = $option[‘label’];
}
//d($attributeArray); exit;
$color_array = array();
$index = 0;
foreach ($attributeArray as $value) {
    $value_index = get_attribute_id($value, ‘color’);
    if ($value != ”) {
        $color_array[$index] = array(
            ‘value_index’ => $value_index,
            ‘label’ => $value,
            ‘is_percent’ => 0,
            ‘pricing_value’ => ‘0’,
            ‘attribute_id’ => ’92’
        );
        $index++;
    }
}
//d($color_array); exit;
$size_attribute = Mage::getModel(‘eav/config’)->getAttribute(‘catalog_product’, 136);
foreach ($size_attribute->getSource()->getAllOptions(true, true) as $option) {
    $sattributeArray[$option[‘value’]] = $option[‘label’];
}
//d($attributeArray); exit;
$size_array = array();
$index = 0;
foreach ($sattributeArray as $value) {
    $value_index = get_attribute_id($value, ‘size’);
    if ($value != ”) {
        $size_array[$index] = array(
            ‘value_index’ => $value_index,
            ‘label’ => $value,
            ‘is_percent’ => 0,
            ‘pricing_value’ => ‘0’,
            ‘attribute_id’ => ’92’
        );
        $index++;
    }
}
//d($size_array); exit;
// exit;
$cProduct = Mage::getModel(‘catalog/product’);
$model = Mage::getModel(‘catalog/product’);
$setConfigurableAttributesData = array(
    ‘0’ => array(‘id’ => NULL, ‘label’ => ‘Color’, ‘position’ => NULL,
        ‘values’ => $color_array,
        ‘attribute_id’ => 92, ‘attribute_code’ => ‘color’, ‘frontend_label’ => ‘Color’,
        ‘html_id’ => ‘config_super_product__attribute_0’
    ),
    ‘1’ => array(‘id’ => NULL, ‘label’ => ‘Clothing Size’, ‘position’ => NULL,
        ‘values’ => $size_array,
        ‘attribute_id’ => 136, ‘attribute_code’ => ‘size’, ‘frontend_label’ => ‘Clothing Size’,
        ‘html_id’ => ‘config_super_product__attribute_1’)
);
//d(($product_sku_collection[‘sku’])); exit;
$index = 1;
foreach ($product_sku_collection[‘sku’] as $key => $value) {
    $main_product_data = $model->load($value[‘simpleprodcollection’][‘0’][‘entity_id’]);
    $main_pd_name = $main_product_data->getData(‘name’);
    $setConfigurableProductsData=array();
    //d($value[‘simpleprodcollection’][‘0’][‘entity_id’]);
    foreach ($value[‘simpleprodcollection’] as $v) {
        $clab = Mage::getModel(‘catalog/product’)->load($v[‘entity_id’])->getAttributeText(‘color’);
        $cvalue_index = Mage::getResourceModel(‘catalog/product’)->getAttributeRawValue($v[‘entity_id’], ‘color’);
        $slab = Mage::getModel(‘catalog/product’)->load($v[‘entity_id’])->getAttributeText(‘size’);
        $svalue_index = Mage::getResourceModel(‘catalog/product’)->getAttributeRawValue($v[‘entity_id’], ‘size’);
        $setConfigurableProductsData = array(
            $v[‘entity_id’] =>
            array(‘0’ =>
                array(‘attribute_id’ => ’92’, ‘label’ => $clab, ‘value_index’ => $cvalue_index, ‘is_percent’ => 0, ‘pricing_value’ => ”),
                ‘1’ =>
                array(‘attribute_id’ => ‘136’, ‘label’ => $slab, ‘value_index’ => $svalue_index, ‘is_percent’ => 0, ‘pricing_value’ => ”)
            )
        );
        //d($setConfigurableProductsData);
    }
    $cProduct
            ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
            ->setTaxClassId(5)
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
            ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
            ->setWebsiteIds(array(1))
            ->setAttributeSetId(4) // You can determine this another way if you need to.
            ->setSku(“C” . $main_product_data->getData(‘confiugrable_sku’))
            ->setName($main_product_data->getData(‘name’))
            ->setQty(25)
            ->setPlayer($main_product_data->getData(‘player’))
            ->setBrand($main_product_data->getData(‘brand’))
            ->setStyle($main_product_data->getData(‘style’))
            ->setShortDescription($main_product_data->getData(‘short_description’))
            ->setDescription($main_product_data->getData(‘description’))
            ->setPrice($main_product_data->getData(‘price’));
    $cProduct->setStockData(array(
        ‘use_config_manage_stock’ => 1,
        ‘is_in_stock’ => 1,
        ‘is_salable’ => 1,
    ));
    $cProduct->setConfigurableProductsData($setConfigurableProductsData);
    $cProduct->setConfigurableAttributesData($setConfigurableAttributesData);
    $cProduct->setCanSaveConfigurableAttributes(1);
    try {
            $cProduct->save();
         $cProduct->unsetData();
        //$productId = $cProduct->getId();
        echo $index.”) “.$cProduct->getId() . “====>” . $main_pd_name . ”   addedn”;
    } catch (Exception $e) {
        echo “$main_pd_name  not addedn”;
        echo “exception:$e”;
    }
    $index++;
}
//****************—————–*****************//
?>

(Visited 80 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.