I have a json. First i have to decode then add loop on a specific key of json.
public function saveRecode() {
$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) {
$this->_logger->debug("enter in opt ");
$this->_logger->info(print_r($arrayOption,true));
$option = $this->_options
->setProductId($_product->getId())
->setStoreId($_product->getStoreId())
->addData($arrayOption);
$option->save();
$_product->addOption($option);
}
}
In database only last recode save. But i want to save all parameters please take a loop.
You're replacing $options
each time through the loop, not adding a new element to it. It should be:
$options = [];
foreach ($jsonDecode["productDetails"] as $key => $value) {
$options[] = [
'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',
]
]
];
}