Chat Room Article Base Facebook Page Twitter RSS Feed
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2012, 12:56:33 PM

Login with username, password and session length
Search:     Advanced search
The E-commerce forum, the best place for advice for your Small Business.
12794 Posts in 1521 Topics by 6461 Members
Latest Member: egrovesystems
* Home Help Search Login Register
+  E-commerce forum
|-+  E-commerce
| |-+  Shopping cart software
| | |-+  Magento
| | | |-+  Creating custom options with csv file
« previous next »
Pages: [1] Print
Author Topic: Creating custom options with csv file  (Read 651 times)
mahesh adodis
Settling In
**

Karma: 0
Offline Offline

Posts: 32


« on: November 22, 2010, 06:47:49 AM »

Hi,
you all might have heared about custom options in magento
for large number of products we cant create it from back end so we can use Csv file to do that this is how i have done

Quote

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.

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.

Here's paired down example of the import 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,Another T-Shirt,6.00,XS|S|M|L|XL


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.

Small|Medium:fixed:5|Large:fixed:10


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


sku,name,description,price,Size:drop_down:1
T-Shirt1,T-Shirt,A T-Shirt,5.00,Small:fixed:0:-SM|Medium:percent:2:-MED|Large:percent:3:-LRG
T-Shirt2,T-Shirt2,Another T-Shirt,6.00,XS:fixed:0:-XS|S:fixed:0:-S|M:fixed:1:-M|L:fixed:1:-L|XL:fixed:2:-XL





Logged
PewDrop
Member (ds)
Settling In
*****

Karma: 1
Offline Offline

Gender: Male
Posts: 69


Dropshipper of Great British Pewter Products


WWW
« Reply #1 on: July 15, 2011, 07:30:06 AM »

Hi,

CSV file never heard about it before, can you brief it please?
This question is from someone that thinks Mag is user friendly?

Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.15 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
SimplePortal 2.2.2 © 2008-2009