Try the Demo

Magento Forum

   
Page 1 of 2
Automatically add options to attributes if they don’t currently exist? 
 
elitriona
Sr. Member
 
Total Posts:  106
Joined:  2008-10-23
 

Here is what I mean:

-I have a spreadsheet ready to import.
-I’ve got a column for colors.
-The color I have in the spreadsheet is BLUE.
-I have not yet created a BLUE option in the color attribute in the back end. If I import the spreadsheet now then BLUE will not be included in the attribute option because it doesn’t exist in the back end.

What would be nice, is if when I import the spreadsheet the system recognizes that I have not yet added BLUE as an option to the back end, and automatically adds that option to the color attribute.

This would save major headaches for people who let’s say have 300 colors they need to add, or 500 manufacturers. Instead of the system not including those entries in the options, it say “oh, it looks like you have options to add to this attribute” and takes care of that automatically.

I know you can add mass info to the database directly, and in a more complex fashion, but ideally you’d want this process to be simple and streamlined. Especially in cases where people have clients who pee their pants at the thought of even seeing a live database, let alone executing anything on it grin

Does anyone know of a way to achieve this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I am currently working on this and have been for a while, but I am not having any luck. This pseudo code is like such:

if($attributeExists(array(’color’, ‘blue’)))
{
$product->setColor(’getAttributeByValue(’color, ‘blue’));
}
else
{
$attribute = getAttribute(’color’);
$attribute->addValue(’blue’);
$product->setColor(’getAttributeByValue(’color’, ‘blue’));
}

I would assume its something like that… I will post my solution when I get it figured out. If anyone knows the methods to call for this, please post!

 
Magento Community Magento Community
Magento Community
Magento Community
 
abijah
Sr. Member
 
Total Posts:  130
Joined:  2008-10-01
 

Seriously. How difficult can it be to program magento to automatically create these attribute options on import. Is there a way to do this via mysql? I found a thread titled: “Import Bulk Attributes"*, this might help.

http://www.magentocommerce.com/boards/viewthread/9391/P0/

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I have created three functions that are extremely helpful in this process. If you are familiar with programming and the mage classes these will be extremely beneficial…

public function attributeValueExists($arg_attribute$arg_value)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_options_modelMage::getModel('eav/entity_attribute_source_table') ;

        
$attribute_code         $attribute_model->getIdByCode('catalog_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;
    
}
    
    
public function addAttributeValue($arg_attribute$arg_value)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_options_modelMage::getModel('eav/entity_attribute_source_table') ;

        
$attribute_code         $attribute_model->getIdByCode('catalog_product'$arg_attribute);
        
$attribute              $attribute_model->load($attribute_code);
        
        
$attribute_table        $attribute_options_model->setAttribute($attribute);
        
$options                $attribute_options_model->getAllOptions(false);
        
        if(!
$this->attributeValueExists($arg_attribute$arg_value))
        
{
            $value[
'option'= array($arg_value,$arg_value);
            
$result = array('value' => $value);
            
$attribute->setData('option',$result);
            
$attribute->save();
        
}
        
        
foreach($options as $option)
        
{
            
if ($option['label'== $arg_value)
            
{
                
return $option['value'];
            
}
        }
        
return true;
    
}
    
    
public function getAttributeValue($arg_attribute$arg_option_id)
    
{
        $attribute_model        
Mage::getModel('eav/entity_attribute');
        
$attribute_table        Mage::getModel('eav/entity_attribute_source_table');
        
        
$attribute_code         $attribute_model->getIdByCode('catalog_product'$arg_attribute);
        
$attribute              $attribute_model->load($attribute_code);
        
                                  
$attribute_table->setAttribute($attribute);
                                  
        
$option                 $attribute_table->getOptionText($arg_option_id);
        
        return 
$option;
    
}

... and ...

public function updateProductColor()
{
    $color 
'blue';
    
$product Mage::getModel('catalog/product')->load($your_product_id);

    
$color_id $this->attributeValueExists('color'$color); //ask magento if the color 'blue' is already an attribute, if it does, return its attribute_id

    
if($color_id//if the color value 'blue' does exist in magento already simply set 
    
{
        $product
->setColor($color_id);
        
$product->save();
    
}
    
else //the color value 'blue' doesn't exist in magento, so add it, then set it
    
{
        $color_id 
$this->addAttributeValue('color'$color);
        
$product->setColor($color_id);
        
$product->save();
    
}


}

My implementation of this is a little different, as I am looping through all products and updating their color from a separate database, so I freehand wrote this out, if there are any mistakes, I will fix them. I hope this helps others, as I spent a great deal of time building this work around. I must also note, this has only been testing in 1.1.6.

I am also including some files that have been extremely helpful to me. I downloaded them here in the forums, but I can’t remember what user submitted them… sorry…

I suppose this could be turned into an extension that would allow a text value to be entered into a dropdown attribute and it would check to see if it already exists, then would associate that attribute to the product… I will see what I can do. I am extremely busy launching our companies new e-commerce site, but as soon as work settles down, I will see if I an bundle this up to an extension.

Let me know if you need anything! Good Luck

File Attachments
addAttributes.php  (File Size: 3KB - Downloads: 604)
Productwithlinks.php  (File Size: 11KB - Downloads: 319)
Productwithlinks-1.php  (File Size: 12KB - Downloads: 318)
simpleProductToConfigurableProduct.php  (File Size: 11KB - Downloads: 279)
 
Magento Community Magento Community
Magento Community
Magento Community
 
abijah
Sr. Member
 
Total Posts:  130
Joined:  2008-10-01
 

BlakeW,

You’re awesome. Unfortunately I would need some documentation along with this in order to implement it. I’m just getting started as a programmer using PHP/MySQL. Once I really learn the ropes I would like to help out as much as possible since I am taking on e-commerce clients and will be using magento for the foreseeable future.

But, for now, I’m a ‘dummy’ and will need further help.

Thanks,
Ab.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShopGuy
Guru
 
Total Posts:  462
Joined:  2008-09-07
 

Thanks for that code. It will be useful to me too.

 Signature 

Monthly Clubs

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 
abijah - 06 January 2009 06:21 PM

BlakeW,

You’re awesome. Unfortunately I would need some documentation along with this in order to implement it. I’m just getting started as a programmer using PHP/MySQL. Once I really learn the ropes I would like to help out as much as possible since I am taking on e-commerce clients and will be using magento for the foreseeable future.

But, for now, I’m a ‘dummy’ and will need further help.

Thanks,
Ab.

If you have any specific questions, please fire away. I am extremely busy releasing a beta version of a new e-commerce site for a very large company, so I am wrapped up pulling 20+ hour days. Once work settles down, I would like to turn this into an extension. I am thinking that the extension will add the ability to load a product and call a function like “$product->setAttributeValue(’color’, $color)” or any dropdown attribute.

 
Magento Community Magento Community
Magento Community
Magento Community
 
elitriona
Sr. Member
 
Total Posts:  106
Joined:  2008-10-23
 

I can see this becoming a vital scenario for a lot of people. Hopefully it’s something they will consider adding to the core software.

 
Magento Community Magento Community
Magento Community
Magento Community
 
husby
Member
 
Avatar
Total Posts:  70
Joined:  2009-01-07
Roseville, MN
 

I agree this would be incredibly helpful.  Right now I’m banging my head about how to import a huge amount of data with 80 different attributes, each having many, many attribute dropdown options.  I am brand new to magento and liking it so far, but getting everything set up is a real pain.

One thing I may do is just use Access with the MySQL connector to (hackishly) copy and paste the attributes and their options into the appropriate tables.

Does anyone know why in the eav_attribute_option_value table, it creates each option value twice, once for store_id=0 and again for store_id=1?  I’m only using one store, but is there another one in the background that I’m not using?  It seems to do this whether I set the Scope to “Global”, “Store View”, or “Website”.

Thanks for all your ideas and help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
husby
Member
 
Avatar
Total Posts:  70
Joined:  2009-01-07
Roseville, MN
 

I have found that store_id=0 is “Admin” and store_id=1 is “Default Store View”.  Of course these would be different if you’ve modified the stores.  So I take it both are necessary to include in the attribute option values for some reason?  Maybe it wouldn’t display in the admin section or in the customer section if you left one of them out?

I’m still figuring out Magento................

 
Magento Community Magento Community
Magento Community
Magento Community
 
abijah
Sr. Member
 
Total Posts:  130
Joined:  2008-10-01
 

I still don’t understand how attributes, attribute sets, and configurable products work, are related, or are made. I mean, I understand it to a certain point, but can someone give me a magento for dummies breakdown of attributes and configurable products. The knowledge base, users guides, and screencasts have not helped as they’re very short and offer very little in the way of examples and [most importantly] a LOGICAL explanation as to how and why things work the way they do. It’s not that I’m stupid, it’s just that I don’t think it’s very well documented, because I’ve followed the screen cast and configurable product instructions and I’m still not seeing any drop down menus on my configurable product page...even though my configurable product creation page is filled out and looks exactly like the one in the screen cast. It’s also very, very hard to get anyone in the forums to explain things like this. No one minds posting code, but when I’ve asked for simple instructions or definitions people literally don’t respond. I’ve posted something like 4 different threads with not a single response in the “How do I” forum.

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

@abijah I think I can psuedo code my whole import process. I have thousands of simple and configurable products and I have developed custom classes for importing, updating, and managing them. I would be more than glad to share my code, explain each step, and answer questions that you may have. I am extremely busy finishing up our site though. I will see if I can document my code better and share my entire library of classes and update scripts by next week. I have been able to automate 100% of the importing, updating, and managing of products, attributes, and their changes from our IBM DB2 business system with over 100,000 skus. I apologize for this taking so long, but I am working 14 - 18 hours a day trying to release our site.

 
Magento Community Magento Community
Magento Community
Magento Community
 
abijah
Sr. Member
 
Total Posts:  130
Joined:  2008-10-01
 

Blake,

Wow. Though I’m pretty much done with my current project, I’m very much interested in what you’ve written. When you’re done, and have time, let me know. Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

Blake - I don’t mean to bug you about this, but have you had anytime to work on this project yet? I am interested in seeing your solution.

 Signature 

Andrew Maury
Nantucket Brand

 
Magento Community Magento Community
Magento Community
Magento Community
 
blakew
Sr. Member
 
Total Posts:  107
Joined:  2008-06-20
 

I will start a new thread and link to my thread in here. I will start writing on and off as I have time today. Hopefully I can write up the whole import process in a day or so.

 
Magento Community Magento Community
Magento Community
Magento Community
 
afmaury
Sr. Member
 
Total Posts:  100
Joined:  2008-01-05
 

That’s great to hear. Thanks.

 Signature 

Andrew Maury
Nantucket Brand

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2