我有一个脚本,用于在Magento中插入产品自定义选项。它对于单个自定义选项运行良好,如果单个产品有多个自定义选项,则仅插入单个自定义选项并丢弃其他自定义选项。
<?php
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
ini_set("display_errors", 1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
// Include Magento application
require_once ( "../app/Mage.php" );
umask(0);
// Initialize Magento
Mage::app("default");
$start = 0;
$totalno =1000;
$row = 0;
$sku = 0;
$baseRowoption = array();
$myarrayoption = array();
if (($handle = fopen("custom_option-final.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle,",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
if($row == 0){
$baseRowoption[$c]=$data[$c];
}else{
if($c==0){
$sku=$data[$c];
}
$myarrayoption[$sku][$row][$baseRowoption[$c]]=$data[$c];
}
}
$row++;
if($row > $totalno){
break;
}
}
fclose($handle);
}
$myarrayoptionfinal = array();
foreach($myarrayoption as $key=>$myarrayoption1){
//if($key != "smartek-rx3044"){ break; }
foreach($myarrayoption1 as $myarrayoption11){
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['input_type'] = $myarrayoption11['input_type'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['required'] = $myarrayoption11['required'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['sort_order'] = $myarrayoption11['sort_order'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['row_title'][] = $myarrayoption11['row_title'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['row_price'][] = $myarrayoption11['row_price'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['row_price_type'][] = $myarrayoption11['row_price_type'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['row_sku'][] = $myarrayoption11['row_sku'];
$myarrayoptionfinal[$key][$myarrayoption11['option_title']]['row_sort_order'][] = $myarrayoption11['row_sort_order'];
}
}
echo "<pre>";
echo "total No : ".count($myarrayoptionfinal);
echo "<br />";
//print_r($myarrayoptionfinal);
$i=0;
foreach($myarrayoptionfinal as $keys=>$myarrayoptionfinal1){
if($i<$start){
$i++; continue;
}
echo "<br />".$i."--->";
$i++;
echo $keys."--->";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$keys);
if($product){
foreach ($product->getProductOptionsCollection() as $option)
{
$optionsData = $option->getData();
$optionsData['is_delete'] = 1;
$product->setProductOptions(array($option->getId() => $optionsData));
$product->setCanSaveCustomOptions(true);
$product->save();
}}else{
continue;
}
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$keys);
print_r($product->getData("entity_id"));
foreach($myarrayoptionfinal1 as $keysub=>$myarrayoptionfinal11){
$subarray = array();
foreach($myarrayoptionfinal11['row_title'] as $keysub1=>$myarrayoptionfinal111){
$subarray[]=array(
'is_delete' => 0,
'title' => $myarrayoptionfinal111,
'price_type' => $myarrayoptionfinal11['row_price_type'][$keysub1],
'price' => $myarrayoptionfinal11['row_price'][$keysub1],
'sku' => $keys.'_'.$myarrayoptionfinal11['row_sku'][$keysub1],
'sort_order' => $myarrayoptionfinal11['row_sort_order'][$keysub1],
'option_type_id'=> -1,
);
}
$required = false;
if($myarrayoptionfinal11["required"] == 1) $required = true;
$opt = array(
'is_delete' => 0,
'is_require' => $required,
'previous_group' => '',
'title' => $keysub,
'type' => $myarrayoptionfinal11["input_type"],
'price_type' => '',
'price' => '',
'sort_order' => $myarrayoptionfinal11["sort_order"],
/** array of values for this option **/
'values' => $subarray,
);
}
try {
//print_r($opt);
$option = Mage::getModel('catalog/product_option')
->setProduct($product)
->addOption($opt)
->saveOptions();
$product->setHasOptions(1);
$product->save();
}
catch (Exception $e) {
echo 'Caught exception 2: '.$e->getMessage()."\n";
continue;
}
}
最佳答案
将自定义选项添加到多个产品非常困难,因为在magento中有多种类型的自定义选项,例如(下拉菜单,文本等),您还将在每个自定义选项中找到需要处理的多个值。
http://www.magentocommerce.com/magento-connect/custom-option-manger.html
该扩展为多种产品提供了确切的功能以及多个自定义选项。