Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
How do we programmically assign a multiple select or dropdown attribute value for a product? 
 
icvu
Jr. Member
 
Total Posts:  10
Joined:  2009-01-25
 

Hi, All—I have been working on how to assigned a manufacturer on a dropdown attribute to a product during product import, but got no where for the past few days.

Basically, I am working on layered navigation, so I created an attribute called “manufacturers”, which is a dropdown type. I assigned all the manufacturers as the values during the creation of the attribute by a separate script.

I have about 2000 products that I need to import and assigned each product a manufacturer from all of the ones available in the “manufacturers” attribute. How do I assign a manufacturer to a product import using direct data import?

$productData is an array containing all the product attributes and their value. My problem is I don’t know how to programatically assign an attribute of type “select” for each product that I import.

For example, if the “manufacturers” attribute of type ‘select’ has the following values, how to I select the second one on the list to use during product import?

manufactuter contains “Sony”, “Samsung”, “JVC”, ...etc.

$productData[$attributeCode]=$attributeValue;

So, what I have don’t and know how is $productData[’manufacturers’] = ‘Samsung’. And this does not work, because when the product is imported the value for the attribute “manufacturer” is “no” not “Samsung”.

$product Mage::getModel('catalog/product');
                
$product->setWebsiteIds(array(1)); 
                
$product->setStoreId(1);
                
$product->setStore(1);
                
$product->setAttributeSetId(isset($productData['attributeSetId']) ? $productData['attributeSetId'9); 
                
$product->setSku($productData['sku']);
                
$product->setTypeId('simple'); 
                
$product->setName($productData['name']);
                
$product->setDescription('this is the description');
                
$product->setShortDescription('This is the short description');
                
$product->setPrice($productData['price']);
                
//if(isset($productData['special_price'])) $product->setSpecialPrice($productData['special_price']);
                
$product->setWeight($productData['weight']);
                
$product->setStatus(1); // enabled
                
$product->setTaxClassId(2); // taxable goods
                
$product->setCategoryIds($productData['category_ids']);
                
$product->setVisibility( isset($productData['visibility']) ? $productData['visibility');
                
$product->addData($productData);
                
$product->save();

I also tried using the product import API. But I do not know how to assign an attribute of type select or dropdown a value. I know how to assign attributes of type ‘text’.

$proxy->call($sessionId'product.create', array('simple',//product type
                                                 
$sets[$category],//attribute set id from the Magento, the attribute set has to exist in Magento otherwise this will break.
                                                           
"$sku",//the sku of the product, the sku has the be inserted as a string
                                                     
$productData //the product data
                                                     
));

At this point it’s getting frustrating because I spent about 3 days on it already. Any help is much appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elachys
Jr. Member
 
Total Posts:  6
Joined:  2009-04-15
 

Would love to see a reply to this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
milenasecret
Jr. Member
 
Total Posts:  5
Joined:  2010-01-27
 

I would also love to see a reply to this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
icvu
Jr. Member
 
Total Posts:  10
Joined:  2009-01-25
 

I got the solution to my own question a day after I posted this question. I figured it’s about time I contribute to the community. Here is a stripped down sample of a class that I developed. This class is to give someone an idea on how to solve this problem. I hope this will help somebody save some times. This is the example of how to add a new value to an attribute of type “select”.

class AttributeOption extends Mage_Eav_Model_Entity_Setup{
    
    
/**
     * @param string|integer $entityTypeId
     * @param string $attributeCode - attribute code
     * @param string|array $attributeLabel - One or a list of attribute values
     *
     */
    
    
public function addOption($entityTypeId$attributeCode,$attributeLabel){
        $attributeId 
$this->getAttributeId($entityTypeId$attributeCode);//retrieve attribute id data by id or code
        
$attribute Mage::getModel('eav/config')->getAttribute('catalog_product'$attributeId );//get the attribute and it's internal data
        
$options $attribute->getSource()->getAllOptions(truetrue);
        
        
$options['value']["$attributeLabel"][0] $attributeLabel;//insert new new option

        
$options['attribute_id'$attributeId;
        
$this->addAttributeOption($options);
    
}
}

Here is how one would assign values to an attribute of type “multiselect”. It’s weird how this works. I pulled my hairs out for two days for this simple answer.

Let’s say we have an attribute of type “multiselect” with the attribute code of “color”.

Assuming this is how we will save the product.

/here is how we set the select option value for our multiselect attribute color
//It does not take an array and there has to be a space before and after the comma
$data['color''12323 , 24243';//<= here is the magic                                                 
$product Mage::getModel('catalog/product');
$product->setWebsiteIds(array(1);
$product->setStoreId(1);
$product->setStore(1);
 .......
$product->addData($data);
 
$product->save();

 
Magento Community Magento Community
Magento Community
Magento Community
 
mrantoniodavid
Jr. Member
 
Total Posts:  19
Joined:  2009-02-14
 

@icvu

Regarding the assignation of multiselect values, this largely worked for me except that I had to omit those spaces before and after the comma.

(Using v1.3.2.4)

Thanks for the help!

 Signature 

-- Antonio
Blog @ NVNCBL
Developer @ Redstage Networks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
701238 users|903 users currently online|497225 forum posts