Checkout

WooCommerce Estimated Delivery Dates

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

This snippet adds some much needed functionality to the WooCommerce shipping methods shown on the checkout page.

Specifically, when adding this code snippet, you are able to add an estimated number of days each shipping method takes until the customer receives their order.

The code then takes the number of days and calculates the estimated delivery date by adding the number of days to the current date.

The date is displayed according to your WordPress settings under Dashboard > Settings > General.

By showing an estimated date of delivery, you provide a more positive user experience as it prevents the need for customers to contact you to ask the approximate delivery date.

/**
 * Snippet Name:	WooCommerce Estimated Delivery Dates For Shipping Methods
 * Snippet Author:	ecommercehints.com
 */

// Create a filter for each shipping method
add_action( 'init', 'ecommercehints_init', 100 );
function ecommercehints_init() {
	$shipping_methods = WC()->shipping->get_shipping_methods();
	foreach ( $shipping_methods as $id => $shipping_method ) {
		add_filter( "woocommerce_shipping_instance_form_fields_$id", 'ecommercehints_create_shipping_method_days_field' );
	}
}

// Create a custom field in shipping method editor named "days" and identify where it will be shown
function ecommercehints_create_shipping_method_days_field( $fields ) {
	$new_fields = array(
		'days' => array(
			'title' => 'Days to arrive',
			'type'  => 'number',
			'desc_tip' => 'The number of days the order will take to arrive with this shipping method.'
		),
	);
	$keys  = array_keys( $fields );
	$index = array_search( 'title', $keys, true );
	$pos   = false === $index ? count( $fields ) : $index + 1;
	return array_merge( array_slice( $fields, 0, $pos ), $new_fields, array_slice( $fields, $pos ) );
}

// Create the days field as meta
add_filter( 'woocommerce_shipping_method_add_rate_args', 'ecommercehints_create_days_as_meta', 10, 2 );
function ecommercehints_create_days_as_meta( $args, $method ) {
	$args['meta_data']['days'] = htmlentities( $method->get_option( 'days' ) );
	return $args;
}

// Calculate the estimated delivery date by adding the number of days to the current date
add_action( 'woocommerce_after_shipping_rate', 'ecommercehints_output_shipping_method_tooltips', 10 );
function ecommercehints_output_shipping_method_tooltips( $method ) {
	$meta_data = $method->get_meta_data();
	if ( array_key_exists( 'days', $meta_data ) ) {
		$days = apply_filters( 'ecommercehints_days_output', html_entity_decode( $meta_data['days'] ), $method );
		if ($days) {
			$current_date = date (get_option( 'date_format' ), strtotime("+" . $days . " days"));
			$html = '<div class="ecommercehints_est_delivery"><small>Estimated Delivery Date: ' . $current_date . '</small></div>';
			echo apply_filters( 'ecommercehints_days_output_html', $html, $days, $method  );
		}
	}
}

How Do I add the number of days to each shipping method?

When the code snippet has been added, simply edit each of your shipping methods and you will see a new meta field “Days to arrive”. In this box, enter the number of days it will take for the order to arrive if the user chooses this option. The code then calculates the estimated delivery date based on the number of days you have entered.

WooCommerce estimated delivery days field when editing a shipping method

Snippet Benefits

  • Show the estimated delivery date on the WooCommerce checkout under the shipping methods.
  • Provide a better User Experience by preventing the need for customers to contact you to enquire about delivery dates.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 10% off on your first month with us.

26027
WELCOME OFFER