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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
$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!.