Fontis: Add product custom options
Product options add an extra level of flexibility for customisation of Magento products, and are particularly useful when configurable products aren't appropriate. Setting custom product options in bulk, such as via an import script, is not currently possible with the Magento DataFlow system, and would be time consuming to do manually. We can create a script in PHP directly that solves this problem, and allows enough flexibility to use for any number of different data sources.
Like rating summaries, custom options are stored separately to the product object. The best way to set them is to first create an array with the data for the option:
$options = array(); $options[$sku] = array( 'title' => 'Option Title', 'type' => 'radio', 'is_require' => 1, 'sort_order' => 0, 'values' => array() ); $options[$sku]['values'][]= array( 'title' => 'Option Value 1', 'price' => 0.00, 'price_type' => 'fixed', 'sku' => '', 'sort_order' => '1' ); $options[$sku]['values'][] = array( 'title' => 'Option Value 2', 'price' => 89.00, 'price_type' => 'fixed', 'sku' => '', 'sort_order' => '1' );
Note that the $options array has been created in this way to allow for multiple products to be filled out at once. Simply change values for more SKUs. This array can be filled out however is most appropriate for your system. This could be from an XML-RPC connection to another service, dynamically calculated values, or read from a CSV file. The sample values used in the above example should map logically to the values presented in the Magento product editor interface.
Once the array of data is set up, the custom options can be added to a product like so:
foreach($options as $sku => $option) { $id = Mage::getModel('catalog/product')->getIdBySku($sku); $product = Mage::getModel('catalog/pr oduct')->load($id); if(!$product->getOptionsRe adonly()) { $product->setProductOption s(array($option)); $product->setCanSaveCustom Options(true); $product->save(); } }
Using the above code you can add custom options to all the products defined in the array that was set up previously. With slight modifications, it can be used to pull values from other sources. If your catalogue includes a lot of products that require custom options then having a script to set these values automatically can save a great deal of time.

