WordPress定制站点:如何将自定义字段整合到下单时间下拉选项
Alright, let's break this down so you can easily add those custom field values to your time dropdown without getting tangled up in JS (since you mentioned you're not super familiar with it—totally get it!). You've already got the core PHP for generating the base time slots, so we just need to plug in the custom field data and merge it all together.
Step 1: Grab the Custom Field Values from WordPress
First, we need to pull the Delivery_time or collection_time values depending on whether the user is choosing delivery or collection. In WordPress, we'll use get_post_meta() (or get_field() if you're using Advanced Custom Fields, which handles data formatting nicer).
Here's how to do it:
// First, figure out if the user is selecting delivery or collection // Adjust this part based on how your site tracks order type—maybe a POST variable or page ID? $order_type = 'delivery'; // Replace with your actual logic to get 'delivery' or 'collection' // Fetch the custom field value if ($order_type === 'delivery') { $custom_times = get_post_meta(get_the_ID(), 'Delivery_time', true); } else { $custom_times = get_post_meta(get_the_ID(), 'collection_time', true); } // If the custom field is stored as a comma-separated string (like "10:00,12:30,15:00"), convert it to an array if (!empty($custom_times) && is_string($custom_times)) { $custom_times = explode(',', $custom_times); // Clean up any extra spaces around each time $custom_times = array_map('trim', $custom_times); }
Note: Double-check the custom field key names—WordPress is case-sensitive here, so make sure Delivery_time or collection_time matches exactly what's set in your site's custom fields.
Step 2: Merge Custom Times into Your $time_option Array
Now that we have the custom times as an array, we can add them right after your existing code that generates the base time slots.
Here's how to integrate it with your existing code:
// Your existing code for generating base time slots if($order_min == 1){ $time_option[] = $hrs.':00 (ASAP)'; $time_option[] = $hrs.':15'; $time_option[] = $hrs.':30'; $time_option[] = $hrs.':45'; } // Add the custom times to the $time_option array if (!empty($custom_times) && is_array($custom_times)) { foreach ($custom_times as $time) { // Optional: Add a label like "(Custom)" to help users distinguish these slots $time_option[] = $time . ' (Custom)'; } }
Step 3: (Optional) Avoid Duplicate Time Options
If there's a chance your custom times overlap with the base slots (e.g., a custom time of 10:00 when your base slot already has 10:00 (ASAP)), you can remove duplicates with array_unique():
// Remove any duplicate time options $time_option = array_unique($time_option);
Step 4: Output the Dropdown as Usual
Finally, your existing foreach loop will handle outputting all the options—base slots plus custom ones—since they're all in the $time_option array now. Just make sure you're escaping the output for security (always a good practice in WordPress):
<select name="delivery_time"> <?php foreach ($time_option as $time) : ?> <option value="<?php echo esc_attr($time); ?>"><?php echo esc_html($time); ?></option> <?php endforeach; ?> </select>
That's it! This should add your custom field time values right into the dropdown alongside the base slots. If you run into issues with the custom field not pulling correctly, double-check the field key and make sure the post ID (from get_the_ID()) is the right one for your product/order page.
内容的提问来源于stack exchange,提问作者user9795198

