0

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

1
  • Easiest way would be to import these options via admin.
    – Prateek
    Commented Mar 15, 2016 at 6:33

2 Answers 2

0

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();
    }
10
  • I am not sure,Please check it no your test site for this
    – Arunendra
    Commented Mar 15, 2016 at 9:35
  • seems adding custom option more that one
    – mdeveloper
    Commented Mar 15, 2016 at 9:42
  • i tested with category having 8 product when i checked category had same 8 costom options instead of one , how to fix it
    – mdeveloper
    Commented Mar 15, 2016 at 9:47
  • Let me check it, I will be back in some time. at the moment i am little bussy
    – Arunendra
    Commented Mar 15, 2016 at 9:49
  • hey i updated code that works fine pls check and approve review
    – mdeveloper
    Commented Mar 15, 2016 at 10:03
1

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.

2
  • it's saving custom option twice on first product and removing all products url key
    – Arunendra
    Commented Mar 15, 2016 at 6:04
  • does not works adds custom option many time like 2,3,4 so on
    – mdeveloper
    Commented Mar 15, 2016 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.