Reputation: 969
I would like programmatically create product with more then one custom option. My current code is bellow but only last option is saved. Can anyone help?
$options = array(
array(
'is_delete' => 0,
'title' => 'First Line',
'type' => 'text',
'is_require' => 0,
'sort_order' => 1,
),
array(
'is_delete' => 0,
'title' => 'Second Line',
'type' => 'text',
'is_require' => 0,
'sort_order' => 2,
)
);
$product = Mage::getModel('catalog/product')->load($id);
$optionInstance = $product->getOptionInstance();
foreach($options as $option){
$product->setHasOptions(1);
if (isset($option['is_require']) && ($option['is_require'] == 1)) {
$product->setRequiredOptions(1);
}
$optionInstance->addOption($option);
$optionInstance->setProduct($product);
$product->save();
}
Upvotes: 2
Views: 8695
Reputation: 1696
In the following example code, I am adding custom option with title ‘Size’ and I have added two values to it ‘Long’ and ‘Short’.
$productId = YOUR_PRODUCT_ID;
$product = Mage::getModel('catalog/product')->load($productId);
$options = array(
'title' => 'Size',
'type' => 'drop_down',
'is_required' => 1,
'sort_order' => 0,
'values' => array(
array(
'title' => 'Long',
'price' => 10.50,
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 0,
),
array(
'title' => 'Short',
'price' => 0,
'price_type' => 'percent',
'sku' => 'test-product-sku',
'sort_order' => 0,
)
)
);
$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);
$product->save();
After that it will look like:-
Upvotes: 0
Reputation: 7611
Here the code is below:
$selectOptionId = 1379;
$selectOptionValueId = 794;
$textOptionId = 1380;
$fileOptionId = 1381;
// Update custom option of Text Field type
$customTextFieldOption = array(
"title" => "Custom Text Field Option Title Updated",
"type" => "field",
"is_require" => 1,
"sort_order" => 20,
"additional_fields" => array(
array(
"price" => 13.00,
"price_type" => "fixed",
"sku" => "custom_text_option_sku_updated",
"max_characters" => 127
)
)
);
Mage::getModel('catalog/product_option_api')->update($textOptionId, $customTextFieldOption, $store = null);
// Update custom option of Dropdown type
$customDropdownOption = array(
"title" => "Custom Dropdown Option Title Updated to Multiselect",
"type" => "multiple",
"additional_fields" => array(
array(
"value_id" => $selectOptionValueId,
"price" => 14.00,
"price_type" => 'percent',
"sku" => "custom_select_option_sku_1 updated",
"sort_order" => 26
)
)
);
Mage::getModel('catalog/product_option_api')->update($selectOptionId, $customDropdownOption, $store = null);
Let me khow ,if it is not working
Upvotes: 2
Reputation: 969
Ok my solution is below:
$options = array(
array(
'title' => 'First Line',
'type' => 'field',
'is_require' => 0,
'sort_order' => 1,
),
array(
'title' => 'Second Line',
'type' => 'field',
'is_require' => 0,
'sort_order' => 2,
)
);
$product = Mage::getModel('catalog/product')->load($id);
$product->setHasOptions(true)->save();
foreach($options as $option_data){
$option = Mage::getModel('catalog/product_option')
->setProductId($product->getId())
->setStoreId($product->getStoreId())
->addData($option_data);
$value = Mage::getModel('catalog/product_option_value');
$value->setOption($option);
$option->addValue($value);
$option->save();
$product->addOption($option);
$product->save();
}
Upvotes: 3