Christopher Shennan's Blog

A day in the life of…

I have recently taken on a new web project which is to move an e-commerce website from a custom 3rd party solution over to Magento and before I even got started I hit a road block (you have got to love Magento for that!).  I found that I had to import around 200 product attributes (colours, sizes, etc) from the old online shop to the magento installation and obviously I did not want to enter all these by hand, but where to begin?!

Luckily I came across a post on the Magento forums which details a method for bulk importing product attributes into Magento and although it did not allow me to automate the whole process, it was sufficient for my purposes.

The sample code provided in the post allows you to import multiple attributes into Magento but you have to create the attribute in Magento manually before you can import the values as the import does not create non-existing attibutes.  The other problem for me was that the sample code only allowed you to import one attribute at a time and then you had to edit files and id’s to do the next one.

As I had multiple attributes to import I tweaked the example code (you can download the tweaked code here) to allow me to import values for multiple attributes at one time.  I still had to create the attributes manually within Magento first but the tweaked code allowed me to import all my attributes values for multiple attributes at once.

In order to import the product attribute values you need to create files in your magento installation within the folder

/var/import/attributes/

The files need to be in the format of [attribute_id]_[attribute_name].csv i.e. 80_color.csv, 122_size.csv, techically the _[attribute_name] is not necessary but it helps to make identifying which attribute the file is for a whole lot easier.

Each value for the product attribute need to be in a separate line and the first line needs to contain the word “admin”.  For example, 80_color.csv contains:-

admin
Red
Blue
Green
Purple
Black

Once you have created the attribute files and copied to other files to their relevant locations you can then open ImpAttributes.php for your Magento installation in your browser i.e. http://www.mysite.com/ImpAttributes.php and all being good, your product attributes should be imported.

I have made the adapted product attributes import for Magento sample code available for download via the link below:-

Sample Code

This also contains a sample colours and sizes file

Importing Product Attributes into Magento – Sample Code

References

Bulk Import Attributes – Original solution provided by Srinigenie

About Christopher Shennan

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for Line Digital in Edinburgh, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Connect with me via Twitter, Google+ or LinkedIn

  • #1 | Written by Ben about 2 years ago.

    I tried to run the script but got the following error:

    Fatal error: Class ‘Mage_Eav_Model_Import’ not found

    I’m on Magento 1.5 – any ideas?

  • #2 | Written by Christopher Shennan about 2 years ago.

    Hi Ben,

    It sounds like the contents of the zip file haven’t been extracted into the correct location. Can you confirm you have extracted the entire contents of the zip file (and kept the file hierarchy) into the root directory of your magento project before running the ImpAttributes.php file?

    Many thanks.

    Chris.

  • #3 | Written by Ben about 2 years ago.

    Your right – thanks! sorry about that silly mistake

    On another note – this script works brilliantly, thanks!

    One note of caution – one of my attribute values looked like this “08 D-Cup|30 D-Cup” – the pipe char in the value caused the script to create 2 x attribute entries with the same ID, which breaks the attribute (ie. you get an error if you try to navigate to that attribute when logged in to admin).

  • #4 | Written by Christopher Shennan about 2 years ago.

    Hi Ben,

    Thanks for highlighting that. The original script I based my code around used the pipe character as a separator between columns (although we are only using 1 column for this) but if you include the value in quotation marks it will treat the attribute value as one value like you had intended. I never had any attribute values with the pipe symbol so I had not come across that until now.

    Many thanks.

    Chris.

  • #5 | Written by slimer about 2 years ago.

    I know is too late but… How can I insert the value for each store? only insert the value on the admin store. I have 2 more stores. The admin store is in website_id 0 and the other 2 are in the website_id 1.

    Thanks in advance.

  • #6 | Written by constance about 2 years ago.

    Thanks, very helpful!

  • #7 | Written by CCBlackburn about 2 years ago.

    Just noticed one thing…if the security is not set correctly for the csv file, the page times out. I needed to add and if check around the fopen call

    if ($file = fopen($this->fileName, “r”))
    {

    }
    else
    {
    echo “file failed to open. Check file permissions”;
    }

  • #8 | Written by Jeremy Blake about 2 years ago.

    You’re the man! Thanks! Wooohooooo

    You just allowed me to avoid 850 lines of manual entry.

  • #9 | Written by Stega about 1 year ago.

    Exactly what I was looking for. I imported 3000 values in less than 5 min. Thanks!!

  • #10 | Written by ray about 1 year ago.

    i wish i could upload the products this way….

  • #11 | Written by Mo about 1 year ago.

    Just wanted to say many thanks for sharing your good work. This script has saved me so much time!!

    Mo

  • #12 | Written by Adam D about 1 year ago.

    This is good information, how would we go about assigning a sort order for these attribute values? My Issue is in magento admin they are listed in alphabetical order – Which is what we like. But if we dont manually assign a sort order they show up randomly on the products.

  • #13 | Written by ray about 1 year ago.

    after importing i get the following:
    “There has been an error processing your request”
    when going to the imported attribute on the magento panel.
    any advice?

  • #14 | Written by Christopher Shennan about 1 year ago.

    Hi Ray,

    Did you manage to get this resolved?

    Many thanks.

    Chris.

  • #15 | Written by Christopher Shennan about 1 year ago.

    Hi Adam,

    Did you manage to figure this out in the end?

    Many thanks.

    Chris

  • #16 | Written by olof about 1 year ago.

    After importing i get the following:
    ————
    There has been an error processing your request
    Exception printing is disabled by default for security reasons.

    Error log record number: 1248097888617
    ————
    when going to the imported attribute on the magento panel.
    I imported two attributes. one I created myself and the other “color” was already default there. The one created my self works fine. so it looks like it has problems with the ones who already exist by default. I use Magento 1.6.2.

    Any advise?

    regards,
    Olof

  • #17 | Written by Dragan about 1 year ago.

    Works fine in 1.7. as well.

  • #18 | Written by d.jones about 1 year ago.

    Does this method work for magento 1.7?

  • #19 | Written by Jeremy about 1 year ago.

    Trying this the second time around, I’m having two issues:

    1. I added another field to the import.csv called “Default Store View” and I just duplicated the names I was trying to import (that I had under the ‘admin’ column). Doing this has somehow corrupted my attribute, and every time I try to open it, I get an error. Looked at the error logs, and either I don’t know what to look for, or there is no useful information.

    Anybody know how to manually delete an attribute in the database, or otherwise?

    2. How can we make the values display alphabetically?

    Thanks
    Jeremy´s last [type] ..Shoemoney Redesigns Blog. Looks Like Every Other Thesis Site on The Web.

  • #20 | Written by shyam about 1 year ago.

    thanks alot.. its working..
    everything is working.. like a charm..
    HUGSSSS

  • #21 | Written by shyam about 1 year ago.

    one more thing..
    u rock dude.. :)

  • #22 | Written by Sammy about 1 year ago.

    Thank you! It is really helpful for me!!
    But, I thought same thing as Slimer(#5).
    Is there any solution to I import the values for each store.
    (I have also two stores; one is English and the other is Chinese.)

Leave a Comment

Subscribe to comments

CommentLuv badge