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 9 months 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 9 months 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 9 months 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 9 months 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 7 months 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 6 months ago.

    Thanks, very helpful!

  • #7 | Written by CCBlackburn about 6 months 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 6 months ago.

    You’re the man! Thanks! Wooohooooo

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

  • #9 | Written by Stega about 4 months ago.

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

  • #10 | Written by ray about 4 months ago.

    i wish i could upload the products this way….

  • #11 | Written by Mo about 4 months 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 4 months 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 4 months 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 3 months ago.

    Hi Ray,

    Did you manage to get this resolved?

    Many thanks.

    Chris.

  • #15 | Written by Christopher Shennan about 3 months ago.

    Hi Adam,

    Did you manage to figure this out in the end?

    Many thanks.

    Chris

Leave a Comment

Subscribe to comments

CommentLuv badge