Social Icons

twitterfacebookgoogle pluslinkedinemail

Saturday, 6 April 2013

How to create simple Product with dropdown attribute in magento


// Set an Admin Session

// Then we see if the product exists already, by SKU since that is unique to each product
$product = Mage::getModel('catalog/product')

      // we will create new product
      $product = new Mage_Catalog_Model_Product();

    $product->setSku('UNIQUE SKU HERE');
    $product->setTaxClassId(0); // class id
    $product->setWebsiteIDs(array(0)); // your website ids
    $product->setStoreIDs(array(0));  // your store ids
        'is_in_stock' => 1,
        'qty' => 99999,
        'manage_stock' => 0,

// set the rest of the product information here that can be set on either new/update
$product->setAttributeSetId(9); // the product attribute set to use
$product->setName('Product Title/Name');
$product->setCategoryIds(array(0,1,2,3)); // array of categories it will relate to
$product->setShortDescription('Short Description');

// set the product images as such
// $image is a full path to the image. I found it to only work when I put all the images I wanted to import into the {magento_path}/media/catalog/products - I just created my own folder called import and it read from those images on import.
$image = '/path/to/magento/media/catalog/products/import/image.jpg';

$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery ($image, array ('image'), false, false);
$product->addImageToMediaGallery ($image, array ('small_image'), false, false);
$product->addImageToMediaGallery ($image, array ('thumbnail'), false, false);

// setting custom attributes. for example for a custom attribute called special_attribute
// custome_attribute will be used on all examples below for the various attribute types
$product->setCustomeAttribute('value here');

// setting a Yes/No Attribute

// setting a Selection Attribute
$attribute_model = Mage::getModel('eav/entity_attribute');
        $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
        $attribute_code = $attribute_model->getIdByCode('catalog_product', $attribut_name);
        $attribute = $attribute_model->load($attribute_code);
        $options = $attribute_options_model->getAllOptions(false);
        // determine if this option exists
        $value_exists = false;
        foreach($options as $option) {
            if (strtolower($option['label']) == strtolower($arg_value)) {
                return   $vid =  $option['value'];

$product->setSpecialAttribute($vid); //specify the ID of the attribute option, eg you creteated an option called Blue in special_attribute it was assigned an ID of some number. Use that number.

// setting a Mutli-Selection Attribute
$data['special_attribute'] = '101 , 102 , 103'; // coma separated string of option IDs. As ID , ID (mind the spaces before and after coma, it worked for me like that)

} catch(Exception $e){
    echo $e->getMessage();
    //handle your error
This code has been testen in magento 1.6.X if you have any error about this code write your comment

1 comment :

  1. Hello I wish to to share a comment here concerning you to definitely be able to inform you just how much i personally Loved this particular study. I have to elope in order to aTurkey Day time Supper but desired to leave ya an easy comment. We preserved you Same goes with be returning subsequent function to read more of yer quality articles. Keep up the quality work.



Free Advertisement

Free Advertisement

Free Advertisement

Free Advertisement