首先申明此文章是我从网络上拷来了,不过我也是从这文章中学会的批量上传产品中的自定义选项(custom options)设置的。

最后的格式上传上去可能和你预想的不一样可以在此留言。

要在你的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: Fieldarea: Areafile: Filedrop_down: Drop-downradio: Radio Buttonscheckbox: Checkboxmultiple: Multiple Selectdate: Datedate_time: Date & Timetime: 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 后变成:
Size:drop_down:1
Small:fixed:0:-SM|Medium:percent:2:-MED|Large:percent:3:-LRG
XS:fixed:0:-XS|S:fixed:0:-S|M:fixed:1:-M|L:fixed:1:-L|XL:fixed:2:-XL

除非注明, askADC博客 的文章均为原创,转载请注明作者和出处。
本文链接地址: http://blog.askadc.com/magento-%e6%89%b9%e9%87%8f%e5%af%bc%e5%85%a5%e8%87%aa%e5%ae%9a%e4%b9%89%e9%80%89%e9%a1%b9custom-options%e8%ae%be%e7%bd%ae/

发表评论

邮箱地址不会被公开。 必填项已用*标注