Thursday 13 October 2016

How to import products with custom options by CSV file in Magento

If you want to import products with custom options by CSV file. So, you need some changes in "app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php" file. 

But it is core file of Magento, so we can not change directly in this file.  Because when you update Magento, then this file will be replace from new file and your customization will be removed.

So, you copy this file on same location in "app/code/local" folder.


app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php

 to 

app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php


Now, open ("app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php") file and add some following codes.


The below line numbers can change in different Magento versions. I used line numbers according to Magento ver. 1.9.2.4.


At about line 734 you will see:

foreach ($importData as $field => $value) {

Just above add following code:

$custom_options = array();


At about line 742 you will see:

$attribute = $this->getAttribute($field);
if (!$attribute) {
   continue;

}



Now need,  add some code above the continue statement.


/* CUSTOM OPTION CODE */
if(strpos($field,':')!==FALSE && strlen($value)) 
{
$values=explode('|',$value);
if(count($values)>0) 
{
@list($title,$type,$is_required,$sort_order) = explode(':',$field);
$title = ucfirst(str_replace('_',' ',$title));
$custom_options[] = array(
'is_delete'=>0,
'title'=>$title,
'previous_group'=>'',
'previous_type'=>'',
'type'=>$type,
'is_require'=>$is_required,
'sort_order'=>$sort_order,
'values'=>array()
);

foreach($values as $v) 
{
$parts = explode(':',$v);
$title = $parts[0];
if(count($parts)>1) {
$price = $parts[1];
} else {
$price =0;
}
if(count($parts)>2) {
$price_type = $parts[2];
} else {
$price_type = 'fixed';
}
if(count($parts)>3) {
$sku = $parts[3];
} else {
$sku='';
}
if(count($parts)>4) {
$sort_order = $parts[4];
} else {
$sort_order = 0;
}

switch($type) 
{
case 'file':

$custom_options[count($custom_options) - 1]['sku'] = $your_custom_sku;
$custom_options[count($custom_options) - 1]['file_extension'] = $your_custom_file_extension;
$custom_options[count($custom_options) - 1]['image_size_x'] = $your_custom_X_size;
$custom_options[count($custom_options) - 1]['image_size_y'] = $your_custom_Y_size;
$custom_options[count($custom_options) - 1]['price'] = $your_custom_price;
$custom_options[count($custom_options) - 1]['price_type'] = $your_custom_price_type;
break;

case 'field':
case 'area':
$custom_options[count($custom_options) - 1]['max_characters'] = $sort_order;

/* NO BREAK */

case 'date':
case 'date_time':
case 'time':
$custom_options[count($custom_options) - 1]['price_type'] = $price_type;
$custom_options[count($custom_options) - 1]['price'] = $price;
$custom_options[count($custom_options) - 1]['sku'] = $sku;
break;

case 'drop_down':
case 'radio':
case 'checkbox':
case 'multiple':
default:
$custom_options[count($custom_options) - 1]['values'][]=array(
'is_delete'=>0,
'title'=>$title,
'option_type_id'=>-1,
'price_type'=>$price_type,
'price'=>$price,
'sku'=>$sku,
'sort_order'=>$sort_order,
);
break;
}
}
}
}

/* END CUSTOM OPTION CODE */



Now, you'll see $product->save(); . Add following code just after.


/* Remove existing custom options attached to the product */
foreach ($product->getOptions() as $o) 
{
$o->getValueInstance()->deleteValue($o->getId());
$o->deletePrices($o->getId());
$o->deleteTitles($o->getId());
$o->delete();
}

/* Add the custom options specified in the CSV import file */

if(count($custom_options)) 
{
foreach($custom_options as $option) {
try {
$opt = Mage::getModel('catalog/product_option');
$opt->setProduct($product);
$opt->addOption($option);
$opt->saveOptions();
}
catch (Exception $e) {}
}

}




That's it. Now, you add all product custom options in CSV file.

For, import a custom option, you have 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, You want to add drop down option called "Size". So your column header should be Size:drop_down:1 (1 for required, 0 for optional). Here is a list of the Types, which is used for "Custom Options" 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

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

Here's example of the import product with custom option format:

sku,name,description,price,Size:drop_down:1

T-Shirt1,T-Shirt,A T-Shirt,5.00,Small|Medium|Large
T-Shirt2,T-Shirt2,B T-Shirt,6.00,XS|S|M|L|XL


Now, if you want to an additional price and SKU for each option value. So, the complete syntax with all option values is:

title:price:price_type[fixed or percent]:sku:sort_order

Small|Medium:5:fixed::1|Large:10:percent:L_10:0


Here's the first example with additional price/sku modifiers.

sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt1,A T-Shirt,5.00,Small:0:fixed:-SM:0|Medium:2:percent:-MED:1|Large:3:percent:-LRG:2

28 comments:

  1. It was a really nice blog. I was looking for this kind of posts regarding Magento. Its really helpful. Thank you for this kind of posts, keep updating.
    buy blue sapphire online

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Very impressive post and pretty well maintained. Really appreciated, Keep updating your post.
    buy white sapphire online 

    ReplyDelete
  4. Very informative post about Magento developing. Good to know about this. Keep updating your post.
    buy opal stone online India

    ReplyDelete
  5. Really good to know about the programming knowledge of magento. I really like this post. Thanks for posting, keep updating.
    Chequered Plate Suppliers

    ReplyDelete
  6. This post is pretty well maintained and this post is so informative. Thanks for this post, keep updating.
    Ecommerce Portals Designing Company In Delhi

    ReplyDelete
  7. Quiet lengthy but very informative post about custom options. Glad to see the posts, keep updating.
    self balancing scooter in delhi

    ReplyDelete
  8. This post impressed me, thanks for the post and keep updating.

    female models Delhi

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! custom leather patches

    ReplyDelete
  11. I was reading your article and wondered if you had considered creating an ebook on this subject. Your writing would sell it fast. You have a lot of writing talent. check this site

    ReplyDelete
  12. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. ינדיאַ טוריסט וויזע

    ReplyDelete
  13. I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. Visto per affari in India

    ReplyDelete
  14. I discovered your blog post site online and check a few of your early posts. Always keep within the really good operate. I additional up your RSS feed to my MSN News Reader. Looking for forward to reading more from you later on!
    visa de canadá para estadounidenses

    ReplyDelete
  15. Nice post mate, keep up the great work, just shared this with my friendz 승인전화없는 토토사이트

    ReplyDelete
  16. I can’t believe focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material without a doubt. It is one of the greatest contents. 먹튀검증

    ReplyDelete
  17. Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can’r wait to read more posts. 오피아트

    ReplyDelete
  18. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value. Im glad to have found this post as its such an interesting one! I am always on the lookout for quality posts and articles so i suppose im lucky to have found this! I hope you will be adding more in the future... 먹튀폴리스

    ReplyDelete
  19. Superior post keeps up with this exceptional work✅. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again!
    Construction and Development

    ReplyDelete
  20. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. get information

    ReplyDelete
  21. I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. website

    ReplyDelete
  22. That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. visit this site

    ReplyDelete
  23. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post! unique names for companies

    ReplyDelete