0

I am trying to save custom options(drop_down type) for a specific product. In my case, I need to add a custom option in the loop.

My code is here

$json ='{"productID":"1","productPrice":"5585.00","productDetails":{"productImage":"https:\/\/development.modeconfigurator.com\/eCommerce\/backdrop.jpg","TABLE TOP":"COPPER DISTRESSED","TABLE FRAME":"RAL 5024 PASTEL BLUE"},"_":"1583172411557"}';
        $jsonDecode = json_decode($json, true);
foreach ($jsonDecode["productDetails"] as $key => $value) {
            $options = [
                    0 =>[
                    'sort_order' => '1',
                    'title' => $key
                    ,
                    'price_type' => 'fixed',
                    'price' => '',
                    'type' => 'drop_down',
                    'is_require' => '0',
                    'values' => [
                        '0' =>[
                            'title' => $value,
                            'price' => '',
                            'price_type' => 'fixed',
                            'sku' => '',
                            'sort_order' => '0',
                            'is_delete' => '0',
                        ]
                        ]
            ]
                ];
            foreach ($options as $arrayOption) {
//
                $option = $this->_options
                    ->setProductId($_product->getId())
                    ->setStoreId($_product->getStoreId())
                    ->addData($arrayOption);
                $option->save();
                $_product->addOption($option);
            }
        }

The output of this code is

enter image description here

But I want to add 3 Custom Options 1 for product image, 2 for Top Table, 3 for Top frame and add 1 value for each custom option(drop_down). loop in screenshot

enter image description here

Please tell me where am I doing a mistake.

1

Try this:

$json = '{"productID":"1","productPrice":"5585.00","productDetails":{"productImage":"https:\/\/development.modeconfigurator.com\/eCommerce\/backdrop.jpg","TABLE TOP":"COPPER DISTRESSED","TABLE FRAME":"RAL 5024 PASTEL BLUE"},"_":"1583172411557"}';
    $jsonDecode = json_decode($json, true);
    $options = [];
    $count = 0;
    foreach ($jsonDecode["productDetails"] as $key => $value) {
        $options[] = [
            'sort_order' => $count++,
            'title' => $key,
            'price_type' => 'fixed',
            'price' => '',
            'type' => 'drop_down',
            'is_require' => '0',
            'values' => [
                '0' => [
                    'title' => $value,
                    'price' => '',
                    'price_type' => 'fixed',
                    'sku' => '',
                    'sort_order' => '0',
                    'is_delete' => '0',
                ]
            ]
        ];
    }
    foreach ($options as $arrayOption) {
        $option = $this->_options
            ->setProductId($_product->getId())
            ->setStoreId($_product->getStoreId())
            ->addData($arrayOption);
        $option->save();
        $_product->addOption($option);
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.