So, I've got the basics of PHP now, and I'm writing a plugin to extend Woocommerce. I've added a custom tab to my product settings, and inside the tab I can add a dynamic number of Custom Charges relating to the product. Essentially, I use JQuery to create a div with 4 select elements inside. I can add any number of div elements (thus, any number of Custom Charges for the product). I've found a way to do this, but I believe there must be a better way (currently I use a 'counter' variable and give each div and select a numeric suffix). I say better way, because when I delete a div (thus delete a 'Custom Charge') I find I'm having to renumber all the existing div and select elements (otherwise my update_post_meta save process will fail.
This is how I'm saving the data currently:
//Additional Charge Types
$i = $_POST['count_of_charge_types']; <-this is a hidden field
$counter = 1;
if ($i>0){
while($counter <= $i){
$add_charge_sub = array("Description" =>
esc_attr($_POST['_add_charge_desc_'. $counter]),"Charge_Type" =>
esc_attr($_POST['_add_charge_type_'. $counter]),"Charge_Value" =>
esc_attr($_POST['_add_charge_value_'. $counter]),"Charge_Method" =>
esc_attr($_POST['_add_charge_method_'. $counter]));
$add_charges[]=$add_charge_sub;
$counter++;
};
update_post_meta($post_id,'_product_add_charges',$add_charges);
This is an example of the HTML so you can understand the naming of the elements and how they get saved:
<div id="add_charges_1" class="options_group">
<div id="add_charges_header_1" class="desc_header">
<button id="but_remove_charge_type_1" type="button">Remove Charge Type</button>
<input type="text" class="short" style="" name="_add_charge_desc_1"
id="_add_charge_desc_1" value="Fuel Surcharge">
</div>
<input type="text" class="short" style="" name="_add_charge_value_1" id="_add_charge_value_1" value="4">
<select id="_add_charge_method_1" name="_add_charge_method_1">
<option value="unit" selected="selected">Per Unit</option>
<option value="group">Per Product Group</option>
</select>
</div>
it works fine, but as I said, when I delete a 'Custom Charge' I might be left with a gap in the numbering of the elements which throws my loops off.
Can anyone point me in the right direction on how to generate the elements in such a way that I can do away with the numbering system and simply serialize the fields.
I've read something about PHP serialization here, and it talks about creating classes. Creating classes is new to me, and I have no idea where to start and whether it's the correct and easiest approach anyway.
The reason I'm looking at classes is because as you can see fromt he update_post_meta code I'm creating arrays and storing them in the DB. I can't just loop over each item in the form using foreach($_POST as $name => $value) because I need the options to stay together in the arrays.
I have another motive for trying to work this out though, and that's because the next stage of development of the site will involve creating a lot of custom options, and I want to make the development quick and efficient.