magento-tutorials

Import products with custom options in Magento

Posted on October 17, 2012 · Posted in Magento Tutorials

We had to import 40,000 products to one Magento store and all the products had options in there. I was shocked to discover that, Magento dont support any import profile for custom options.
I look around and found someone had posted a fix, which worked straight. Here is the hack :

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.

At about line 566 you will see:

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

Just above that add:

        $custom_options = array();

At about line 575 you will see:

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

You will need to 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_type = $parts[1];
         } else {
            $price_type = 'fixed';
         }
         if(count($parts)>2) {
            $price = $parts[2];
         } else {
            $price =0;
         }
         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':
               /* TODO */
               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 further on in the code (about line 710) you’ll see $product->save(); Just after this add the following code:

/* 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, you should be all set to import custom product options.