Change The Minimum Delivery Date Dynamically

Scenario

Your store normally delivers on the same day (i.e. your minimum delivery date is set to “0”). However, you have one product that requires 4 days to prepare. As such, you want to ensure that the minimum selectable date is set to “4” if a customer has that product in their cart.

Example of minimum delivery date in WooCommerce

Solution

You can use the iconic_wds_min_delivery_date filter to modify the minimum selectable date based on items in the cart (or anything else you like).

/**
 * Change minimum delivery date.
 *
 * If a certain product is in the cart (ID 100), add
 * 4 days to the minimum selectable delivery date.
 *
 * @param array $min
 *
 * @return array
 */
function iconic_change_min_delivery_date( $min ) {
	if ( empty( WC()->cart ) ) {
		return $min;
	}

	// The product ID to check for in the cart.
	$product_id = 70;

	// If the product is not in the cart, return the original data.
	if ( ! iconic_is_product_in_cart( $product_id ) ) {
		return $min;
	}

	// Add 4 days.
	$days_to_add = 4;

	// This filter returns an array containing the days to add and a timestamp.
	return array(
		'days_to_add' => $days_to_add,
		'timestamp'   => strtotime( "+" . $days_to_add . " day", current_time( 'timestamp' ) ),
	);
}

add_filter( 'iconic_wds_min_delivery_date', 'iconic_change_min_delivery_date' );

/**
 * Is product in cart?
 *
 * @param $product_id
 *
 * @return bool
 */
function iconic_is_product_in_cart( $product_id ) {
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( $cart_item['product_id'] == $product_id ) {
			return true;
		}
	}
	return false;
}

Drop this code into your child theme’s fucntions.php file.

There are 2 functions in play here. The first is iconic_change_min_delivery_date

This is the function that modifies the minimum selectable date and is called using add_filter below the function itself.

Within this function we assign a product ID of 70 to the $product_id variable. 

This ID is then run through the other function, iconic_is_product_in_cart. This will return true or false, depending on whether that product is in the cart. 

Note: It’ll need some modifications to work for variations.

Rather than hardcoding this number, you could loop through cart items and check whether they have a specific custom meta, or belong to a specific category, etc. This is just a simplified example.

If it isn’t in the cart, we return $min which is the original data for the filter.

Otherwise, we set the days to add to 4 and build out the array of data that the filter requires.

Now if that product is in the cart, 4 days are added to the minimum selectable date.

Notes

It’s worth noting that you can do exactly the same for the maximum selectable date using the iconic_wds_max_delivery_date filter.

WooCommerce Delivery Slots

Choose a delivery date and time for each order. Add a limit to the number of allowed reservations, restrict time slots to specific delivery methods, and so much more.

Was this helpful?

Please let us know if this article was useful. It is the best way to ensure our documentation is as helpful as possible.