0

Using tutorial I have managed to add dynamic rows with 2 column:

1 - Customer Group

2 - Price for customer

Both fields showing in system config and saving as well.

issue is when I check in database it is showing like:

{"_1611305135170_170":{"customer_group_id":"32000","price_for_customer":"23"},"_1611305140170_170":{"customer_group_id":"1","price_for_customer":"444"}}.

When I checked system configuration for minimum allowed qty at Store > Configuration > Catalog > inventory > Minimum Qty Allowed in Shopping Cart, it is saving values in database like:

{"0":1,"3":20,"2":3}

How can I achieve same save technique ?

Thanks in advance!

4
  • I guess you need to apply some json functions (get nested objects) to extract the desired values, and then you store it in your $data before saving the model. Jan 22, 2021 at 11:57
  • @AbdulSamadAbbasi, could you please give some code example ?
    – Pawan
    Jan 22, 2021 at 12:07
  • Can you write how exactly would you like this to be saved in DB. I suppose {"0":1,"3":20,"2":3} is just an example? Jan 22, 2021 at 12:11
  • it is used by magento when save minimum allowed qty for diff customer group.
    – Pawan
    Jan 22, 2021 at 12:16

1 Answer 1

1

That's one way I'm gonna show, maybe there would be some pre-defined functions that I'm not aware of. You can search about them

$obj= '{"_1611305135170_170":{"customer_group_id":"32000","price_for_customer":"23"},"_1611305140170_170":{"customer_group_id":"1","price_for_customer":"444"}}';
    $obj = json_decode($obj,true);
    $newarr = [];
       foreach ($obj as $key=>$value) {
           {$temp = 0;
               foreach ($value as $nestedkey=>$nestedval)
               { 
                if($nestedkey == "customer_group_id"){
                    $temp = $nestedval;
                }else
                   $newarr[$temp] = (double)$nestedval;
               }
           }    
       }echo json_encode($newarr);

the $obj will be your $data['field_name'] in Save controller before saving the model.

1
  • Thank you for your answer but I need to change how the data is saving!
    – Pawan
    Jan 22, 2021 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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