如何将自定义选项(custom options)导入Magento

By admin, 2010年06月27日 6:57 上午

转载请附上转载地址:http://www.magentochina.org/bbs/viewthread.php?tid=233
在过去的四个月里,我建了四个网店都是用的Magento。我遇到了很多问题,都需要略微的修正。

最近我需要导入200个新的产品。这些产品都有3到4个自定义选项。而标准的Magento导入不允许你导入商品的自定义选项,所以我自己定制了导入的设定并使其允许导入自定义选项。

要在你的Magento网店中加入这个定制,首先要做的是:
复制 app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php  这个文件到app/code/local/Mage/Catalog/Model/Convert/Adapter/Product.php.
这会防止自动更新撤回你的改动。

第二步,你需要加入一些代码到 local 那个版本的Product.php下(code/local/Mage/Catalog/Model/Convert/Adapter/Product.php).

下面的行号是 Magento 1.3版的。 新的版本可能会有点不一样,行数会靠后些,请注意查找。
在大约566行的时候你会看到:
foreach ($importData as $field => $value) {

在这行上面加入:
$custom_options = array();

在大约575行的时候你会看到(这个代码有2次,请查看后面一次的代码):
$attribute = $this->getAttribute($field);
if (!$attribute) {
continue;
}

You will need to add some code above the continue statement.

在  continue 这个语句的上面,需要加入下面这些代码:


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’:

break;

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

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;
}
}
}
}


现在移到大概710行的位子,你会看到  $product->save();
就在这后面,加入下面的代码:


foreach ($product->getOptions() as $o) {
   $o->getValueInstance()->deletue($o->getId());
   $o->deletePrices($o->getId());
   $o->deleteTitles($o->getId());
   $o->delete();
}

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) {}
   }
}

就是这样了,现在一切就绪准备导入自定义产品选项了。

要导入一个自定义选项,你需要在你的CSV导入文件中添加新的一列。新列的名字决定了该选项的名称和类型。格式应该是: Name:Type:Is Required.   (名称:类型:是否必需)。
例如,要创建一个必需的下拉式选项,名称为“Size”,那么列标题应该为:
Size:drop_down:1  (1表示必需,0表示可选)
这是类型的一列。他们会在Magento 管理员界面中的”cumstom options”出现。(原句是: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

而类型一般都有多种值(drop_down下拉式, radio 单选, checkbox 复选框, multiple 复合式),你可以用一个   |   的分隔符来指定多种值。 例如:小,中,大   你可以用 “小|中|大” 作为你csv文件里 “Size:drop_down:1″ 一列的值。

这里有一个导入格式(原句是:Here’s paired down example of the import format。  对于paried down 可以理解为用逗号分开的相对应的列标题和值)

  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

另外你可以为每一个自定义选项值指定一个额外的 价格和 SKU 。这个的语句是这样的:
Value:[fixed|percent]:price_modifier
例如,假设你有一个产品,如果是中号的话,价格会上涨5元,如果是大号的话,上涨10元,你就可以用下面的值作为一个自定义选项的值:
Small|Medium:fixed:5|Large:fixed:10

在第一个例子加上额外的 价格/ SKU 后变成:
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

---------------
这有一个CSV 文件的样本,我常常用来测试附加代码。
我希望这个修正能对那些需要导入产品自定义选项的人有所帮助。

(我用了excel表格的html代码,可惜在文章中显示不出来。我直接把代码贴到这里了,上面也有CSV文件样本的地址,大家自己点进去看吧。)

  1. http://public.sheet.zoho.com/publish/brianmoney/import-sample]http://public.sheet.zoho.com/publish/brianmoney/import-sample

---------------

原文请见:How to import products with custom options in Magento

Leave a Reply

Panorama Theme by Themocracy