Question

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.

No correct solution

OTHER TIPS

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);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top