Which was used in one of my projects to import product data including custom options. Another solution would be writing a script which could dynamically add custom options to a set of existing products. In this post I will show you how to create such script.
The available properties are as follows:
- title – the option’s label;
- type – option type, possible values:
- field – simple input text,
- area – multiline text area,
- file – file with an upload form in the front-end,
- drop_down – drop-down list with predefined values,
- radio – a set of radio buttons,
- checkbox – multiple selectable checkbox options,
- multiselect – list of multiple selectable options,
- date – date input field,
- date_time – date and time combined,
- time – time only;
- is_require – a flag indicating if the option is required, 1 or 0;
- price – price surcharge for the option;
- price_type – type of the option surcharge, fixed or percent (of the product’s price);
- sku – SKU for the option.
Some of the properties are type-dependent, e.g., a “file” option also has:
- file_extension – a string of comma-separated file extensions that are accepted by the option; if empty any extension except “exe” and “php” is allowed;
- image_size_x and image_size_y – image dimension limits in pixels;
An option of type “field” can have the following property:
- max_characters – maximum allowed number of characters.
I will demo a example.
I have a json string as below:
$json = '[ { "title": "Field test 01", "type": "field", "is_require": 1, "price": 10.00, "price_type": "fixed", "sku": "custom_field_option" }, { "title": "Area test 01", "type": "area", "is_require": 1, "price": 10.00, "price_type": "percent", "sku": "custom_area_option" }, { "title": "File test 01", "type": "file", "is_require": 0, "price": 5.00, "price_type": "fixed", "sku": "custom_file_option", "file_extension": "png,jpg", "image_size_x": "800", "image_size_y": "600" }, { "title": "Dropdown test 01", "type": "drop_down", "is_require": 0, "values": [ { "title": "Dropdown row 01", "price": 11.00, "price_type": "fixed", "sku": "custom_dropdown_option_1" }, { "title": "Dropdown row 02", "price": 12.00, "price_type": "percent", "sku": "custom_dropdown_option_2" }, { "title": "Dropdown row 03", "price": 12.00, "price_type": "fixed", "sku": "custom_dropdown_option_3" } ] }, { "title": "Radio test 01", "type": "radio", "is_require": 1, "values": [ { "title": "Radio option 01", "price": 11.00, "price_type": "fixed", "sku": "custom_radio_option_1" }, { "title": "Radio option 02", "price": 12.00, "price_type": "percent", "sku": "custom_radio_option_2" }, { "title": "Radio option 03", "price": 12.00, "price_type": "fixed", "sku": "custom_radio_option_3" } ] }, { "title": "Checkbox test 01", "type": "checkbox", "is_require": 1, "values": [ { "title": "Checkbox option 01", "price": 11.00, "price_type": "fixed", "sku": "custom_checkbox_option_1" }, { "title": "Checkbox option 02", "price": 12.00, "price_type": "percent", "sku": "custom_checkbox_option_2" }, { "title": "Checkbox option 03", "price": 12.00, "price_type": "fixed", "sku": "custom_checkbox_option_3" } ] }, { "title": "Multiple test 01", "type": "multiple", "is_require": 1, "values": [ { "title": "Multiple option 01", "price": 11.00, "price_type": "fixed", "sku": "custom_multiple_option_1" }, { "title": "Multiple option 02", "price": 12.00, "price_type": "percent", "sku": "custom_multiple_option_2" }, { "title": "Multiple option 03", "price": 12.00, "price_type": "fixed", "sku": "custom_multiple_option_3" } ] }, { "title": "Date test 01", "type": "date", "is_require": 0, "price": 5.00, "price_type": "fixed", "sku": "custom_date_option" }, { "title": "Date Time test 01", "type": "date_time", "is_require": 0, "price": 5.00, "price_type": "fixed", "sku": "custom_date_time_option" }, { "title": "Time test 01", "type": "time", "is_require": 0, "price": 5.00, "price_type": "fixed", "sku": "custom_time_option" } ]'; $product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'test-product'); if($product) { //convert json to array $options = json_decode($json, true); $optionInstance = $product->getOptionInstance()->unsetOptions(); $product->setHasOptions(1); foreach($options as $option){ if(!isset($option['details'])) { $option['details'] = $option['title']; } //$product->setRequiredOptions(1); $optionInstance->addOption($option); } $optionInstance->setProduct($product); $product->save(); } else { echo 'Product is not exist!'; }
Result:
Thanks for watching!.