On the checkout page, there are delivery methods, if a specific delivery method (only 1) is selected, then a drop-down list with the delivery time should appear. This field is implemented using the ACF plugin with Repeater and the field type is Text. The time selected by the user should be displayed in the order details in the WooCommerce admin panel.
Based on the theme code that I got, I wrote the next code:
- In the woocommerce / checkout / form-shipping.php file the following code:
<div id="delivery_times" class="form-group d-none">
<label class="text-bold" for="select_time">Delivery time: </label>
<select id="select_time" class="form-control mb-1">
<option value="" selected>Select time</option>
<?php
$delivery_times = get_field('delivery_times', 'options');
$count = 0;
foreach ($delivery_times as $delivery_time):
echo '<option value="'.$delivery_time['range_time'].'" data-pickup="'.$count.'">'.$delivery_time['range_time'].'</option>';
$count++;
endforeach;
?>
</select>
</div>
- There is a common theme-checkout.js file, in which I added the code:
$("#shipping_speed").change(function(){
var delivery_time = $(this).children("option:selected").data("type");
if(delivery_time == "tomorrow"){
$("#delivery_times").removeClass("d-none");
}else{
$("#delivery_times").addClass("d-none");
$("#select_time option[value='']").prop('selected',true);
}
});
And now I need to save the selected data and display it somehow in the order details for the administrator, but I don’t found the implementation of something similar in my theme. Please tell me how I can save this data from the ACF field to the order details?
2
Answers
Data probably it’s saved on another php file, it’s using the MVC model.
One thing I would say you is that it’s a form, so it does have already other data processed, I would suggest you to find where it does process other data and add some properties there…
Makes sense?
Updated:
First on your html
<select>
the attibutename="select_time"
is missing, so replace the line:by this line:
Now you can use the following code snippet to save the chosen value as order meta data:
Then to display it in admin single order pages under shipping address:
Then you can display this delivery time on customer orders and email notifications:
Code goes in functions.php file of the active child theme (or active theme). It should work.