Say I have 1000 product in some category, Now I want to add custom options in it, How can it be done?
Below in my custom option structure:
Title Input Type Is Required Sort Order
size Dropdown No
sizes(S, M, L, XL) and so on
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communitySay I have 1000 product in some category, Now I want to add custom options in it, How can it be done?
Below in my custom option structure:
Title Input Type Is Required Sort Order
size Dropdown No
sizes(S, M, L, XL) and so on
I have Tested @Adarsh code there is a bug in his code, So I have corrected and posted here:
1: changed is_required to is_require to solve required bug 2: added $optionInstance = $product->getOptionInstance()->unsetOptions();
to unset option or duplicate options will be added
<?
$options = array(
'title' => 'Sizes',
'type' => 'drop_down',
'is_require' => 1,
'sort_order' => 0,
'values' => array(
array(
'title' => 'S',
'price' => 0,
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 1,
),
array(
'title' => 'M',
'price' => 0, //your price
'price_type' => 'percent',
'sku' => 'test-product-sku',
'sort_order' => 2,
),
array(
'title' => 'L',
'price' => 0, //your price
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 3,
),
array(
'title' => 'XL',
'price' => 0, //your price
'price_type' => 'percent', //price type %
'sku' => 'test-product-sku',
'sort_order' => 4,
)
)
);
$category_id = 302;//put your cat_id here
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection();
//loop to all products
foreach($products as $product){
$product = Mage::getModel('catalog/product')->load($product->getId());
$optionInstance = $product->getOptionInstance()->unsetOptions();
$product->setHasOptions(1);
if (isset($option['is_require']) && ($option['is_require'] == 1)) {
$product->setRequiredOptions(1);
}
$optionInstance->addOption($options);
$optionInstance->setProduct($product);
$product->save();
}
Try this code:
$options = array(
'title' => 'Sizes',
'type' => 'drop_down',
'is_required' => 1,
'sort_order' => 0,
'values' => array(
array(
'title' => 'S',
'price' => 0, //your price
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 1,
),
array(
'title' => 'M',
'price' => 0, //your price
'price_type' => 'percent',
'sku' => 'test-product-sku',
'sort_order' => 2,
),
array(
'title' => 'L',
'price' => 0, //your price
'price_type' => 'fixed',
'sku' => '',
'sort_order' => 3,
),
array(
'title' => 'XL',
'price' => 0, //your price
'price_type' => 'percent', //price type %
'sku' => 'test-product-sku',
'sort_order' => 4,
)
)
);
$category_id = your_category_id;
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection();
//loop to all products
foreach($products as $prd){
//$product = Mage::getModel('catalog/product')->load($prd->getId());
//un-comment above line if below line doesn't work.
$product = $prd;
//comment above line if you have uncommented first line.
$product->setProductOptions(array($options));
$product->setCanSaveCustomOptions(true);
$product->save();
}
This will set custom option to all products from specific category.
I haven't tested, but should work.