我已经以编程方式添加了带有自定义选项的产品。当我编辑产品&保存从已保存产品中删除的已添加自定义选项。
以下是我使用自定义选项插入产品的代码:
$p = array(
'sku_type' => 0,
'sku' => 'SKU00'.$row['Pid'],
'name' => $row['ProductName'],
'description' => $row['Description'],
'short_description' => $row['Description'],
'type_id' => 'simple',
'attribute_set_id' => 4,
'weight' => 0.00,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'price' => $row['SPrice'],
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => 2,
'store_id' => 1,
'website_ids' => 1,
'tax_class_id' => 0,
);
$product->setData($p);
$optionRawData = array();
$optionRawData[0] = array(
'required' => 1,
'option_id' => '',
'position' => 0,
'type' => 'select',
'title' => $row['ProductName'],
'default_title' => $row['ProductName'],
'delete' => '',
);
$selectionRawData = array();
$selectionRawData[0] = array();
$selectionRawData[0][] = array(
'product_id' => $row['Product_Id'],
'selection_qty' => 1,
'selection_can_change_qty' => 100,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => $row['SPrice'],
'option_id' => '',
'delete' => ''
);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
// Set the Bundle Options & Selection Data
$product->setBundleOptionsData($optionRawData);
$product->setBundleSelectionsData($selectionRawData);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->getResource()->save($product);
$productID = $product->getId();
$product = Mage::getModel('catalog/product')->load($productID);
$optionsArray = array(
array(
'title' => 'Standard',
'price' => 0.0,
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 0,
),
array(
'title' => 'Premium',
'price' => 10,
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 1,
),
array(
'title' => 'Deluxe',
'price' => 20,
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 2,
)
);
$options = array(
'title' => $product->getName(),
'type' => 'radio',
'is_required' => 1,
'sort_order' => 0,
'values' => $optionsArray,
);
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
$optionInstance->addOption($options);
$optionInstance->setProduct($product);
$product->save();