|
10 | 10 | use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
|
11 | 11 | use Magento\Catalog\Api\Data\ProductInterface;
|
12 | 12 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 13 | +use Magento\Catalog\Model\Config\Source\ProductPriceOptionsInterface; |
13 | 14 | use Magento\Catalog\Model\Product\Attribute\Source\Status;
|
| 15 | +use Magento\Catalog\Model\Product\Option as CustomOption; |
| 16 | +use Magento\Catalog\Model\Product\Option\Value as CustomOptionValue; |
14 | 17 | use Magento\Catalog\Model\Product\Type;
|
15 | 18 | use Magento\Catalog\Model\Product\Visibility;
|
16 | 19 | use Magento\Framework\DataObject;
|
@@ -198,21 +201,57 @@ private function prepareOptions(array $data): array
|
198 | 201 | {
|
199 | 202 | $options = [];
|
200 | 203 | $default = [
|
201 |
| - 'product_sku' => $data['sku'], |
202 |
| - 'title' => 'customoption%order%%uniqid%', |
203 |
| - 'type' => ProductCustomOptionInterface::OPTION_TYPE_FIELD, |
204 |
| - 'is_require' => true, |
205 |
| - 'price' => 10.0, |
206 |
| - 'price_type' => 'fixed', |
207 |
| - 'sku' => 'customoption%order%%uniqid%', |
208 |
| - 'max_characters' => null, |
| 204 | + CustomOption::KEY_PRODUCT_SKU => $data['sku'], |
| 205 | + CustomOption::KEY_TITLE => 'customoption%order%%uniqid%', |
| 206 | + CustomOption::KEY_TYPE => ProductCustomOptionInterface::OPTION_TYPE_FIELD, |
| 207 | + CustomOption::KEY_IS_REQUIRE => true, |
| 208 | + CustomOption::KEY_PRICE => 10.0, |
| 209 | + CustomOption::KEY_PRICE_TYPE => ProductPriceOptionsInterface::VALUE_FIXED, |
| 210 | + CustomOption::KEY_SKU => 'customoption%order%%uniqid%', |
| 211 | + CustomOption::KEY_MAX_CHARACTERS => null, |
| 212 | + CustomOption::KEY_SORT_ORDER => 1, |
209 | 213 | 'values' => null,
|
210 | 214 | ];
|
| 215 | + $defaultValue = [ |
| 216 | + CustomOptionValue::KEY_TITLE => 'customoption%order%_%valueorder%%uniqid%', |
| 217 | + CustomOptionValue::KEY_PRICE => 1, |
| 218 | + CustomOptionValue::KEY_PRICE_TYPE => ProductPriceOptionsInterface::VALUE_FIXED, |
| 219 | + CustomOptionValue::KEY_SKU => 'customoption%order%_%valueorder%%uniqid%', |
| 220 | + CustomOptionValue::KEY_SORT_ORDER => 1, |
| 221 | + ]; |
211 | 222 | $sortOrder = 1;
|
212 | 223 | foreach ($data['options'] as $item) {
|
213 |
| - $option = $item + ['sort_order' => $sortOrder++] + $default; |
214 |
| - $option['title'] = strtr($option['title'], ['%order%' => $option['sort_order']]); |
215 |
| - $option['sku'] = strtr($option['sku'], ['%order%' => $option['sort_order']]); |
| 224 | + $option = $item + [CustomOption::KEY_SORT_ORDER => $sortOrder++] + $default; |
| 225 | + $option[CustomOption::KEY_TITLE] = strtr( |
| 226 | + $option[CustomOption::KEY_TITLE], |
| 227 | + ['%order%' => $option[CustomOption::KEY_SORT_ORDER]] |
| 228 | + ); |
| 229 | + $option[CustomOption::KEY_SKU] = strtr( |
| 230 | + $option[CustomOption::KEY_SKU], |
| 231 | + ['%order%' => $option[CustomOption::KEY_SORT_ORDER]] |
| 232 | + ); |
| 233 | + if (isset($item['values'])) { |
| 234 | + $valueSortOrder = 1; |
| 235 | + $option['values'] = []; |
| 236 | + foreach ($item['values'] as $value) { |
| 237 | + $value += [CustomOptionValue::KEY_SORT_ORDER => $valueSortOrder++] + $defaultValue; |
| 238 | + $value[CustomOptionValue::KEY_TITLE] = strtr( |
| 239 | + $value[CustomOptionValue::KEY_TITLE], |
| 240 | + [ |
| 241 | + '%order%' => $option[CustomOption::KEY_SORT_ORDER], |
| 242 | + '%valueorder%' => $value[CustomOptionValue::KEY_SORT_ORDER] |
| 243 | + ] |
| 244 | + ); |
| 245 | + $value[CustomOptionValue::KEY_SKU] = strtr( |
| 246 | + $value[CustomOptionValue::KEY_SKU], |
| 247 | + [ |
| 248 | + '%order%' => $option[CustomOption::KEY_SORT_ORDER], |
| 249 | + '%valueorder%' => $value[CustomOptionValue::KEY_SORT_ORDER] |
| 250 | + ] |
| 251 | + ); |
| 252 | + $option['values'][] = $value; |
| 253 | + } |
| 254 | + } |
216 | 255 | $options[] = $option;
|
217 | 256 | }
|
218 | 257 |
|
|
0 commit comments