3 added 72 characters in body
source | link
$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 $product$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();
}
$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 $product){
    $product->setProductOptions(array($options));
    $product->setCanSaveCustomOptions(true);
    $product->save();
}
$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();
}
2 added 15 characters in body
source | link
$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' => 01,
                                ),
                                array(
                                    'title' => 'M',
                                    'price' => 0, //your price
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 02,
                                ),
                                array(
                                    'title' => 'L',
                                    'price' => 0, //your price
                                    'price_type' => 'fixed',
                                    'sku' => '',
                                    'sort_order' => 03,
                                ),
                                array(
                                    'title' => 'XL',
                                    'price' => 0, //your price
                                    'price_type' => 'percent', //price type %
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 04,
                                )
                            )
            );


$category_id = your_category_id;
$products = Mage::getModel('catalog/category')->load($category_id)
     ->getProductCollection();

//loop to all products 
foreach($products as $product){
    $product->setProductOptions(array($options));
    $product->setCanSaveCustomOptions(true);
    $product->save();
}
$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' => 0,
                                ),
                                array(
                                    'title' => 'M',
                                    'price' => 0, //your price
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 0,
                                ),
                                array(
                                    'title' => 'L',
                                    'price' => 0, //your price
                                    'price_type' => 'fixed',
                                    'sku' => '',
                                    'sort_order' => 0,
                                ),
                                array(
                                    'title' => 'XL',
                                    'price' => 0, //your price
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 0,
                                )
                            )
            );


$category_id = your_category_id;
$products = Mage::getModel('catalog/category')->load($category_id)
     ->getProductCollection();

//loop to all products 
foreach($products as $product){
    $product->setProductOptions(array($options));
    $product->setCanSaveCustomOptions(true);
    $product->save();
}
$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 $product){
    $product->setProductOptions(array($options));
    $product->setCanSaveCustomOptions(true);
    $product->save();
}
1
source | link

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' => 0,
                                ),
                                array(
                                    'title' => 'M',
                                    'price' => 0, //your price
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 0,
                                ),
                                array(
                                    'title' => 'L',
                                    'price' => 0, //your price
                                    'price_type' => 'fixed',
                                    'sku' => '',
                                    'sort_order' => 0,
                                ),
                                array(
                                    'title' => 'XL',
                                    'price' => 0, //your price
                                    'price_type' => 'percent',
                                    'sku' => 'test-product-sku',
                                    'sort_order' => 0,
                                )
                            )
            );


$category_id = your_category_id;
$products = Mage::getModel('catalog/category')->load($category_id)
     ->getProductCollection();

//loop to all products 
foreach($products as $product){
    $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.