How to use WooCommerce to only allow next day delivery if before 1pm and day after tomorrow if ordered after 1pm. For a client, we were working as WooCommerce developers. One of the problem was this simple request.
We tried to find a switch for this in settings but couldn’t. We also didn’t want to install a whole WooCommerce plugin for this small functionality. So ended up writing this simple code. You may use it in the functions.php to set WooCommerce for next day delivery.
add_action('woocommerce_thankyou', 'custom_woocommerce_delivery_dates');
function custom_woocommerce_delivery_dates($order_id) {
// Grab the order
$order = wc_get_order($order_id);
// Loop through order items
foreach ($order->get_items() as $item_id => $order_item) {
// Check if delivery date meta exists
if (wc_get_order_item_meta($item_id, 'Delivery Date', true)) {
$delivery_date = wc_get_order_item_meta($item_id, 'Delivery Date', true);
// Adjust delivery date based on current day and time
if (date('D') == 'Fri' && date('H') > 13) {
$modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +3 day'));
} elseif (date('D') == 'Sat') {
$modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +2 day'));
} elseif (date('H') > 13) {
$modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +2 day'));
} else {
$modified_delivery_date = date('Y-m-d', strtotime($delivery_date . ' +1 day'));
}
// Update the delivery date meta
wc_update_order_item_meta($item_id, 'Delivery Date', $modified_delivery_date);
}
}
}
Implementation and Customization
To integrate this code, simply add it to the functions.php
file of your WordPress theme. Customize the code to meet specific client settings, such as delivery date metadata field names or time-sensitive conditions.
Advantages
Tailored to Client Needs: This code snippet allows WooCommerce developers to create a bespoke solution for clients without the need for extensive plugins or complex configurations. It provides a streamlined and focused approach to address specific requirements.
Efficient Order Processing: By automating the adjustment of delivery dates based on order times, the code ensures accurate and efficient processing, minimizing the chances of errors in delivery estimations.
Enhanced User Experience: Customers benefit from a transparent and user-friendly interface, clearly reflecting delivery options based on their order placement time. This contributes to increased satisfaction and loyalty.
Conclusion
Empowering WooCommerce for nuanced delivery scenarios doesn’t always require extensive plugins. Our code snippet provides a tailored solution for next-day and day-after-tomorrow deliveries based on order times. As WooCommerce developers, optimizing functionality with concise, client-specific code ensures a seamless user experience without unnecessary complexity.