需要导入200个新的产品。这些产品都有3到4个自定义选项。而标准的Magento导入不允许你导入商品的自定义选项,所以我自己定制了导入的设 定并使其允许导入自定义选项。
要在你的Magento网店中加入这个定制,首先要做的是:
复制 app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php
这会防止自动更新撤回你的改动。
第二步,你需要加入一些代码到
下面的行号是 Magento 1.3版的。 老的版本可能会有点不一样。
在大约566行的时候你会看到:
foreach ($importData as $field => $value) {
在这行上面加入:$custom_options = array();
在大约575行的时候你会看到:
$attribute = $this->getAttribute($field);
if (!$attribute) {
continue;
}
在
(粘贴的时候请把 / *
/ * 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’:
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;
}
}
}
}
/ * END CUSTOM OPTION CODE * /
现在移到大概710行的位子,你会看到
就在这后面,加入下面的代码:
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) {}
}
}
将自定义选项(custom options)批量导入Magento(一)续(二)
就是这样了,现在一切就绪准备导入自定义产品选项了。
要导入一个自定义选项,你需要在你的CSV导入文件中添加新的一列。新列的名字决定了该选项的名称和类型。格式应该是:
例如,要创建一个必需的下拉式选项,名称为“Size”,那么列标题应该为:
Size:drop_down:1
这是类型的一列。他们会在Magento
·
·
·
·
·
·
·
·
·
·
而类型一般都有多种值(drop_down下拉式, radio
这里有一个导入格式(原句是:Here’s paired down example of the import format。
sku,
T-Shirt1,
T-Shirt2,
另外你可以为每一个自定义选项值指定一个额外的
Value:[fixed|percent]:price_modifier
例如,假设你有一个产品,如果是中号的话,价格会上涨5元,如果是大号的话,上涨10元,你就可以用下面的值作为一个自定义选项的值:
Small|Medium:fixed:5|Large:fixed:10
在第一个例子加上额外的
sku,
T-Shirt1,T-Shirt, A T-Shirt,
T-Shirt2,T-Shirt2,Another T-Shirt,6.00,