AI Verified

Name

WooCommerce Estimated Delivery Dates

About

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.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Simply copy the code and paste as a new code snippet using the Code Snippets plugin.

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.youtube.com/watch?v=JihnIX-PCio

Codevault

ecommercehints

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.ecommercehints.com/

Snippet Source:

https://www.ecommercehints.com/woocommerce-estimated-delivery-dates

History

Last modified:

06/07/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Cross Site Scripting
Found on line : 45
Code : echo(apply_filters( 'ecommercehints_days_output_html', $html, $days, $method )
Vulnerable line : 45
Code : $html = '<div class="ecommercehints_est_delivery"><small>Estimated Delivery Date: ' . $current_date . '</small></div>');

Potential vulnerability found : Cross Site Scripting
Found on line : 45
Code : echo(apply_filters( 'ecommercehints_days_output_html', $html, $days, $method )
Vulnerable line : 42
Code : $days = apply_filters( 'ecommercehints_days_output', html_entity_decode( $meta_data['days'] ), $method ));

Found 2 vulnerabilities

WooCommerce Estimated Delivery Dates

 
                    
1/**
2 * Snippet Name: WooCommerce Estimated Delivery Dates For Shipping Methods
3 * Snippet Author: ecommercehints.com
4 */
5 
6// Create a filter for each shipping method
7add_action( 'init', 'ecommercehints_init', 100 );
8function ecommercehints_init() {
9 $shipping_methods = WC()->shipping->get_shipping_methods();
10 foreach ( $shipping_methods as $id => $shipping_method ) {
11 add_filter( "woocommerce_shipping_instance_form_fields_$id", 'ecommercehints_create_shipping_method_days_field' );
12 }
13}
14 
15// Create a custom field in shipping method editor named "days" and identify where it will be shown
16function ecommercehints_create_shipping_method_days_field( $fields ) {
17 $new_fields = array(
18 'days' => array(
19 'title' => 'Days to arrive',
20 'type' => 'number',
21 'desc_tip' => 'The number of days the order will take to arrive with this shipping method.'
22 ),
23 );
24 $keys = array_keys( $fields );
25 $index = array_search( 'title', $keys, true );
26 $pos = false === $index ? count( $fields ) : $index + 1;
27 return array_merge( array_slice( $fields, 0, $pos ), $new_fields, array_slice( $fields, $pos ) );
28}
29 
30// Create the days field as meta
31add_filter( 'woocommerce_shipping_method_add_rate_args', 'ecommercehints_create_days_as_meta', 10, 2 );
32function ecommercehints_create_days_as_meta( $args, $method ) {
33 $args['meta_data']['days'] = htmlentities( $method->get_option( 'days' ) );
34 return $args;
35}
36 
37// Calculate the estimated delivery date by adding the number of days to the current date
38add_action( 'woocommerce_after_shipping_rate', 'ecommercehints_output_shipping_method_tooltips', 10 );
39function ecommercehints_output_shipping_method_tooltips( $method ) {
40 $meta_data = $method->get_meta_data();
41 if ( array_key_exists( 'days', $meta_data ) ) {
42 $days = apply_filters( 'ecommercehints_days_output', html_entity_decode( $meta_data['days'] ), $method );
43 if ($days) {
44 $current_date = date (get_option( 'date_format' ), strtotime("+" . $days . " days"));
45 $html = '<div class="ecommercehints_est_delivery"><small>Estimated Delivery Date: ' . $current_date . '</small></div>';
46 echo apply_filters( 'ecommercehints_days_output_html', $html, $days, $method );
47 }
48 }
49}

1

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Order note per product.

Added: 1 year ago

Last Updated: 1 year ago

this code add order note on a single product page and include information in order meta.

WooCommerce

AI Verified

0

WooCommerce Cart Total Based Banner

Added: 1 year ago

Last Updated: 1 year ago

This guide shows you how you how to show a banner on your WooCommerce site based on the cart total. In our example, we show a banner letting the customer know their cart total is over 2K therefore qua...

WooCommerce

AI Verified

1

Disable admin description editor for WooCommerce products

Added: 2 years ago

Last Updated: 1 year ago

Hides the description editor, if you don't have any use for it

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

7

WooCommerce Show Number Of People Viewing Product

Added: 1 year ago

Last Updated: 1 month ago

We want to make this solution absolutely clear from the outset. This snippet does NOT generate the actual number of people viewing the product. It shows a randomly generated number in the range of two...

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 months ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

3

WooCommerce Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 2 months ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...