Disable Delivery Date Based On Chosen Shipping Method
It’s possible to disable specific delivery dates based on the selected shipping method. You will need to add a code snippet to your child theme’s functions.php file:
/**
* Modify dates based on shipping method.
*
* @param array $available_dates
* @param string $format
* @param bool $ignore_slots
*
* @return array
*/
function iconic_modify_delivery_dates( $available_dates, $format, $ignore_slots ) {
global $jckwds;
$chosen_method = $jckwds->get_chosen_shipping_method();
if ( 'flat_rate:3' === $chosen_method ) {
// Remove these dates if chosen method is flat_rate:5.
$remove = array( '20210102', '20210103' );
// If the format is not array, then we need to cast the
// "remove" dates to the required format.
if ( 'array' !== $format ) {
foreach ( $remove as $key => $ymd ) {
$remove[ $key ] = date( $format, strtotime( $ymd ) );
}
}
foreach ( $available_dates as $key => $available_date ) {
$date = 'array' === $format ? $available_date['ymd'] : $available_date;
if ( ! in_array( $date, $remove, true ) ) {
continue;
}
unset( $available_dates[ $key ] );
}
}
// `array_values()` resets the array keys so the default date is index "0".
return array_values( $available_dates );
}
add_filter( 'iconic_wds_available_dates', 'iconic_modify_delivery_dates', 10, 3 );This method does require knowing the selected shipping method value. This is more often than not in the format [method_name]:[instance_id] (for example, flat_rate:5).
To find the method name and instance ID, you can do the following:
- Go to WooCommerce > Delivery Slots > General Settings.
- In the Shipping Methods section, right-click on one of the shipping method checkboxes.
- Click Inspect to open the browser inspector.
- This will reveal the input field and its value. The value is the shipping method you want to use in the code above:
