Thursday, May 7, 2009

How to import products with custom options in Magento

Over the past several months I've been using Magento on four stores. I've run into a few things which have required slight modifications.

Recently I needed to import 200 new products. The products all had between 3 and 4 options. The default Magento import does not allow you to import custom options for products so I customized the import to allow imporing of options.

To add this customization to your magento store first copy app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php to app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php. This will prevent upgrades from overwriting your changes.

Next you need to add some code to "local" version of Product.php (code/local/Mage/Catalog/Model/Convert/Adapter/Product.php). The line numbers below correspond to version 1.3 they may be a bit different in older versions.

To import a custom option you need to add a new column to your CSV import file. The name of the column determines the name and type of the option. The format is: Name:Type:Is Required. For example, to create a required drop down option called "Size" your column header should be: Size:drop_down:1 (1 for required, 0 for optional). Here is a list of the Types, these are taken from the "Custom Options" screen in the Magento admin area.

field: Field

area: Area

file: File

drop_down: Drop-down

radio: Radio Buttons

checkbox: Checkbox

multiple: Multiple Select

date: Date

date_time: Date & Time

time: Time

For types with multiple values (drop_down, radio, checkbox, multiple) you can specify using a | separator. Example for Small, Medium, Large you would use "Small|Medium|Large" as the value for the "Size:drop_down:1" column in your csv file.

In addition you can specify an addition price and SKU modifier for each option value. The syntax for this is Value:[fixed|percent]:price_modifier. For example if you have a product which costs $5 more for a Medium and $10 more for a large you would the following as the option value.

thank you for your post. It was really helpful.Is it possible when importing options to remove the existing ones so to avoid duplicates? If your example is imported twice all custom options will be repeated. Can you please suggest a way to first delete the stored in the database custom options and then write the ones in the CSV import file. Perhaps this should be done just before the saving of the imported options but I don't have idea how to code that.

1. When i run the import and look at my items there do not have a sort Order. How Can i set this for a. The Custom Option Title/Groupb. for the Sub Items

2. If i have a large spreadsheet of products with some having one type of custom-option an another not using that custom-option. The Program still will create that option even if i have not set any data in that field.Is there a way you can do a pre-check to see if the field has data then create the custom option group. Else skip over creating the custom-option group?

so ive modified brians code to now accept a sort order. In both the header and details elements. But I still cant work out how to ignore fields where there is no record. The code will still create the header custom option

thanks. the sort order stuff is cool, as the code above works (abliet not quite so elegant!).

my main issue is where you have a large product file where some fields dont have values. The code still creates the Custom Option Header, but because there is no data in that field the line items are left blank.

So im really just stuck as to how i can first check the field contents has data if yes then create the Custom Option Header and then the line items

Brian,What I mean is adding the brand name to products and not creating an extra category. ie Sony TVs or LG TVs. Sony or LG would be a brand that I would attach to a certain group of products in the TV category.

Hi thanks for the post, Im not sure if Im missing something simple. I have inserted the custom code in to the product.php file, and added your sample custom data in the csv file, but I keep getting the "Notice: Undefined variable: custom_options in /homepages/17/d282805926/htdocs/theultimatedress6/app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php on line 732" error. Ps I had nothing in the app/code/local/ folder so I change the product.php in the app/code/core/Mage/Catalog/Model/Convert/Adapter/ folder and when that didnt work, I created all the additional folders in the local folder, and put the product.php in there. It must be reading the new code, but Im not getting any success. Any suggestions?

Also I did manage to get it to import a couple of times without any errors, but the custom options didn't appear on the front end. PS I had some other custom code in the product.php adding additional gallery images, but I removed it hoping that it would work without with no success

Ok I found out that I hadnt put a 1 in the hasoptions field. Another question, I can get the radio and dropdown buttons, but I just want to ad a text field, I need to have people input a colour number, this is the heading (Colour Number:field:1) I tried putting a 0 and a 1 in the field, but it doesn't show> Have you had success with text fields?

You can create an attribute called "brand" (Catalog->Attributes->Manage Attributes). Then just add a "brand" column to your import. My install had a "manufacturer" attribute already setup, it's a "drop-down" type so you have to predefine the list of values.

First, I forgot to put the code to initialize the $custom_options array in the post, I've added it above.

Second, I never tested with text fields as I don't have product with text input options. The code was not handling them properly. I've updated the code to properly handler field and area types. Your column name will work properly. The input options have slightly different parameters than radio, select, etc.

You have to specify a value for the option to be created, to create an input option with default values use: ":" as the vale you just have to put something in the row so it's not ignored. Here's the format:"Title(not used):price_type:price:sku:max_characters"

Example: :fixed:5:MONGRM:25

This would create an input field with a fixed price of $5, a SKU modifier of "MONGRM" and a max of 25 characters.

Hi brian i have added the code you provided its working perfect but when i import my file having one product only it adds one extra product itself and also process all product at the time of importing but it should process only those product which are specified in csv file . Is there any idea ?

How much more is involved in getting the file upload custom option working. I noticed the comment in the section is just "TODO". I'm trying to code it in myself but any advice you have would be appreciated.

I just found a fix for mass importing configurable products before I found your post. It seems your solution is much simpler. Could this solution be used to add more custom options to the configurable products already loaded into magento? Or would your fix overwrite the current configurable products and options?

I am building a store in Spanish and English. The Custom Options load properly and the only trouble that I am having with it's that the English custom options are being loaded as default(admin) and the values that I have assigned in the spreadsheet for the Spanish store do not get loaded.

I have already specified to which store I want to load the CSV file in the import profile. But this does not seem to work as well. Anyone has any idea or has experienced loading custom options in multiple languages?

Hello Brian !! Thanks so much for your post. I was looking for something like that for a long time!

Well, I'm starting to do all modifications and I realize that I do not have this folders app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php. Can I create that to work on it? I'm working on 1.2.1.2 version.I'll wait your repply!

thanks for this but indeed, any chance to get this working for imported xml? would really need it, tried to do it like it is instructed but to xml, can't see attributes :( Please someone help if you just can

I am having someone develop an online store using magento software.It is a distributor who is loading about 2000 of their products. I have found another distributor with about 20,000 items that I would like to add to the store.The new dist supplied me with an excel database (without images). The items are grouped in columns with appropriate categories,subcategories, manufacturer, product id, description, upc, list price, my disc price, etc.Some of the categories and subs are not in magento from the original dist. Do I need to set up these cat and subs first?I am also interested in adding more items from a 3rd distributor.All orders will be sent to me and in turn I will order from the appropriate dist.There may be instances when I will need to order from 2 different dist.Is there a way to see this on the order? Does the dist field need to be in the database before importing?I am not familiar with website programing but eventually want to learn how to do all this on my own.

Hello, this is a great modification, thanks for it! I'm having a problem getting the custom options to show for configurable products. When I run an import of a configurable product, the custom options do not show. They are entered into the database, but the main entity record in the database for the configurable product sets the "has_options" field to 0. I do have it set to 1 in my spreadsheet though. I've added some debug code into the import script and before $product->save() is called "has_options" is set to 1 but after $product->save() it is set to 0. It looks like $product->save() is resetting the "has_options" field. Can anyone help? I can't seem to find out why. :(

im having the same issues with my magento installation. the product options get imported but they dont show up in the frontend (are visible in the backend). once i save the product in the administration the options also show up in the frontend. any clues?

I've followed each step to the letter and this is not working for me. Import succeeds but ignored the custom options in the csv. Ive tried both with my own csv and with the sample one provided here.As Ronald's post above says it works fine in the latest version so I'm baffled as to why its not working for me! Any pointers??

My problem appears to be similar to what a few people have encountered. The import sort of works but requires you to go into admin, edit each item and add a custom option then clicking save. only now does the imported options show.

also when not following the process ive mentioned, customers cannot purchase the products what should have custom options, when they add to basket it says something along the lines of 'custom options required' but these options aren't visible to the user!

I'd like to use this hack together with a great script that CSV-imports configurable products (see http://www.magentocommerce.com/boards/viewthread/49620/). I merged the code from this page into my product.php but didn't succed. No errors but no custom options either.

There are a couple people in these comments that report this problem importing custom options for configurable products - so it might be not related to CSV-import hack for configurables.

I'm so in need of being able to add thumbnail images to my custom options.Ex if a custom option is set to color I would like to be able to add a color swatch images for black,white etc this feature should have been worked up along with a contribute like yours in a magento release I dont know why its not. I to also would like to be able to export the file once I create a simple product to get a feel on all the columns. I would greatly appriciate your help on accomplishing this I'm so in need.

How to import custom options in two languages for two store views? it doesn't work when I put store instead of default - english for example. All custom options are replaced by the latest file uploaded and visible just in one language for both stores. When the overwriting code in product.php is not commented out then I see both languages in the same time. Any solution?

I had an issue updating our product catalogue with new prices - Magento accepted the new prices (CSV containing SKU and Price), but also cleared all custom options.

To resolve this, I restored the database backup I took before updating the prices, renamed app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php to app/code/local/Mage/Catalog/Model/Convert/Adapter/_Product.php so it would use the original import script, then did the import again and it worked.

Is there a bug in this code that could be removed, in case anyone else experiences the same issue?

Oh, thanks for the script by the way, saved a lot of time when I first imported all the products!

I have 2 custom options, they are uploading geat but 1t duplicates them, so i always have 2 of each, but if i upload a third or forth time it stays at just 2 duplicates. so i think the code you added to delete current option before upload works but not just right, any ideas?

It works in 1.4.1, but as I said above, I didn't get the input type "file" to work. There are a couple of things left out above, but nothing that keeps it from working. Like, it's not obvious (to me) exactly how to enter the data in the csv for sort order to work. Through just trying stuff, I figured out that for sort order of the different custom options, in the csv header, type name:input-type:required:sort-order. e.g. Color:drop_down:1:6 (which is required and the 6th custom option down). While almost the same is true for the product row. name:price-type:price:sku-modifier:sort-order. e.g. Blue:fixed:2:_blue:2 which means blue is 2 dollars extra, adds _blue to the end of the products sku and is the 2nd option within the custom options for Color.

Oh, use a pipe to separate different options. e.g. White:::_white:1|Blue:fixed:2:_blue:2|Red:fixed:3_red:3 means white doesn't cost extra (neither fixed nor a price is needed but the colon separators are needed if you want sku modifiers and/or sort orders). If you just type White|Blue|Red, then they will not cost extra, they will not modify the sku and they will sort alphabetically.

thx for ur post.I have another problem here.Before importing the csv file, we have to map the fields in admin to their respective fields so that the data will go in correct columns.So, in the case of custom options, to which field we will need to map the newly created custom option column?

Nice... Importing custom options is great but how could we combine tier pricing along with categories?. I've seen quite a few attempt it within the forms. This would create the most AWESOME import available!...

Hi, This is exactly what I need, I tested it on my test site and it worked fine, but I have now put the code into the live site and it is not working. The import works without any errors, I have got the 'has_options' set to 1, but there is no custom options on the admin side or front end side.

I still can't get the "file" option to work. Everything else worked as expected. I really need file uploads as a custom option. Please help if you can. Feel free to pm me at magento's site to gw468 if you need to.

Thank you very much for your code.I was wondering if you could consider writing a small extension to enable products eraseby importing a line in the CSVexample : Put a SKU and write "DELETE" in a specific field would mean that you want this product to be erased.

It would be necessary to look for the product options and attribute so as to delete them properly.

I'm not familiar enough with Magento to do so but I'd be glad to donate for your time.

Not having the 'options_container' column populated (try "Product Info Column" or "Block after Info Column") could cause the entire 'buy box' to disappear from the product page after import.

2. The 'required_options' column in the 'catalog_product_entity' table does not get set when one of the custom options is a required option.

I found a very minor impact when you don't have this column set: adding a product directly to the cart from the product list generates a red error message instead of a green "notice" message. I don't know what else could be impacted by not having this column set so I fixed it anyway.

This is fantastic. I can't believe it's not in the core Magento product yet.

Can I ask - did you get anywhere with the export function? I'm going to be giving this to shop data administrators ... and to ask them to maintain a spreadsheet as well as export/import is probably beyond them - and too risky.

Would be great if I could export with one click, and import with one click, with no spreadsheet step :-)

My problem appears to be similar to what a few people have encountered. The import works great but requires you to go into admin, edit each item and add a custom option then clicking save. only now does the imported options show.

also when not following the process ive mentioned, customers cannot purchase the products what should have custom options, when they add to basket it says something along the lines of 'custom options required' but these options aren't visible to the user!

Just implementing Magento myself. Very important subject. This and simply having Categories be defined in a starting upload. The problem w/ Magento is there are 1000 addons to this and that but then you get a mash up instead of a working system.

Glad to see you doing this work. Hopefully Magento (or someone here) will get this integrated in future versions of the software.

I always be aware about the last product launched to the market. I really love to know about new thing, how ever i looking for the quality, for me is very important. In the same line, the service costumer is important too, because i like the efficient in the company. Thats happen when i buy viagra, i never have to wait long time.

This blog is abosolutely wondeful, i like the new ideas introduced here, it catched my attention since the first time that i saw it. i must to say i usually like the good products specially the ones whic can give a lot of benefits. That is why i am proving costa rica investment opportunities

Worked for me - just add the filetypes as the fifth part like this::fixed:35.00:some_sku:1:tif,jpg,pdf

I don't need the image-size, as I'm not dealing with images, but it would be easy to add that as well.Just add two more parts, image-size-x and image-size-y and parse them like the filetypes-part. The corresponding fields in the database are called image_size_x and image_size_y.

To everyone that had the problem for re-save the product in order to see the custom options in the fornt page, just do the followings:in your CSV file under options_containerwrite down : Block after Info ColumnThis solve the problem for meHope that it help averyone (my version 1.4.1.1)By the way, it's about time to add this to the core of Magento!

Just wanted to say, this post is gold. Just finished importing a bunch of items with over 400 options (value:price:sku:sort) each. no problem at all 1.4.1.0. We'll see how well it handles the next set with 1,700 options each. Imagine having to create the options manually... Thanks Brian

Hello,this good idea has been integrated in the extension Import_Products_categories__multiple_images_and_custom_optionsand also in the magmi project as plugin.But, as some people here, I don't understand how to customize a custom option for each store views.In fact, more generally, I see nothing in the syntax to be able to change texts of a custom option for different store views corresponding to different langages. Is there no other solution than doing it manually ?

I have read so much about the has_options in this blog and I know, that I have to set the has options to 1. Otherwise, like it is in my case and many other cases in this blog, i only can see my imported custom options, when i first activate the custom option direcly in the product. And here is my question:WHERE AND HOW CAN I SET THE CUSTOM OPTIONS TO 1?

Thank you; Tobi from Germany (i have the German Language Version of Magento 1.4.1.1

in former times, i didn't need the colum "has_options" for csv imports, so i cancelled this colum. But when you start with importing custum options, you also need the colum "has_options" in your csv import.

I realize that 1.5 now supports Custom Options however the way they have set up the input in the CSV is far more complicated than your method. They require a separate column and row for each of the option titles. Your way is what I have been using for the past year and prefer it. Is there any chance you will modify your code to work with 1.5? Me developer tried copying over your existing code into 1.5 and came up with the following: (I copied the code into the proper place in the latest version. However there is a new problem. The latest version of Magento is checking your file for errors before allowing it to be imported. Strictly speaking this is a good thing. It however is a bad thing in regards to our custom options dealio.)

This really helps.I guess all you have to do is to specify a value for the option to be created, to create an input option with default values use: ":" as the vale you just have to put something in the row so it's not ignored.

Hundreds of ecommerce systems exist, but very few can match the power of Magento. Just like reading to instructions online but nothing beats to the one you provided which is detailed and accurate. And like looking for a good e-commerce site is like looking for Double Roller Blinds that fits your needs and lifestyle.

Thanks for such a useful post, it helps but i got stuck while importing configurable products with custom attributes and if you can provide any dummy csv file that import configurable products, that will be really a great help. Please help me!!

Sorry , but i'm having trouble with this, I think I have put the code in correctly, but when i go to import the csv it still doesn't recognise the Size:drop_down:1 field. Can I send someone my products.php file to check for me? I would be very grateful.

didn't work for me as well, however I'm not such computer guy. Found another way to import products along with options, categories and other things like that - it is a software program, something like "magento store manager" or so (don't remember exactly) - as for me it was working perfectly when I tried it. There's one big disadvantage - it is not free and my trial period expired... Guess this might be useful for someone anyway

Magmi (http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Main_Page) can import product with custom options using the custom options plugin based on the syntax described in this blog (with some enhancements).It can do this fast and it's free.

i've got this error when trying to run import profile script within another maegnto website:

Fatal error: Call to undefined method Mage_Catalog_Model_Product_Attribute_Backend_Media::addImagesWithDifferentMediaAttributes() in /home/arredpit/public_html/app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php on line 810

murthy full info about what in specific? this article may be considered as pure enlightment on this matter of massive importing. Everything is so well explained. i've found that the author has made wven corrections to the originalpost so that everyone can attain to the perfection of this script, if we can call it that way. Post or write bout uour need. Help someoneand you'll get help.

hiwould you be able to help me i followed the instructions to the letter and also made sure has_options colomn has 1 in it, but it isnt showing the options im using magento community 1.6.1. email me a getitonfancydress@hotmail.co.uk

it works fine. It has its consequences though. It happened to me that i started working for http://www.arredo-shop.it.- The used to input every single product and customize options manually as magento wants. They had about 2300 products.- I made the configuration to the file and started importing greatly some 1500 new products.- Then they asked me change prices of all products using the same "batch" strategy (formerly they used to change them prices manually, and that is one by one...argghh)

So they prepared the new prices using my 2 columns "comma separated" open office file and was just ready to import them. (one column for the "sku" and one for the "price")

WHAT HAPPENED?All "new" products (imported by me with my script) where flawlessly updated....All 2300 "old products" (the ones they customized manually so patiently..)

simply L O S T THEIR CUSTOM OPTIONS!

so just b e aware.... awful issue. Learn from this story to avoid losing your job.. help others understand..

I need to change order the configurable product dropdown in front end. There is a option in admin panel but client requires that order is default or we have to give the option in csv import file. I tried lot but not yet get done. So I planned to update the position field directly in "catalog_product_super_attribute" table when importing product. Anyone can you tell where is insert query..?

Hi, I'm having a problem with Magento 1.7.0.2:I can't get it working. I had to create new folders for the local PHP file and I changed the file like you wrote. Now, when importing the products, there's only one product that gets the curstom values. All the others don't. Please help me!

Magento developer should keep the daily update in website development and make a great ideas to build a unique style the website.That is the good responsibility for being expert in magento development field.Magento Development bangalore

I have a color-swatches-for-custom-options extension with me. I can upload the custom option details with MAGMI (Color:drop_down...etc), but I don't know how to upload the custom option images with MAGMI. if you know way to do it. Please let me know. this one is TOP URGENT!!!!