Documentation

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 ) {
	$product_cart_id = WC()->cart->generate_cart_id( $product_id );
	$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

	return ! empty( $in_cart );
}

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.

Are you using WooCommerce Delivery Slots yet?

Start your 14-day free trial right now and get instant access to the features described in this article.

or learn 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.

WooCommerce Delivery Slots

WooCommerce Delivery Slots

Not got the plugin yet? Get instant access right now, free for 14 days.

or learn more

Still need help?

If you haven't found what you're looking for in our documentation, please contact support.

Get Support