AI Verified

Name

Add Woo Delivery Date Field

About

Add a mandatory delivery date field between 2 to 7 days from today.

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

WebSquadron

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.4

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.

History

Last modified:

23/01/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(!empty($delivery_date)
Vulnerable line : 45
Code : $delivery_date = "<strong>Delivery Date:</strong><br>" . get_post_meta($order->get_id(), 'cwpai_delivery_date', true)); // Get custom order meta

Found 1 vulnerabilities

Add Woo Delivery Date Field

 
                    
1// Add delivery date field to the checkout
2add_action("woocommerce_after_order_notes", "cwpai_add_delivery_date_field");
3function cwpai_add_delivery_date_field($checkout)
4{
5 $min_date = date('Y-m-d', strtotime('+2 days')); // 2 days from today
6 $max_date = date('Y-m-d', strtotime('+7 days')); // 7 days from today
7 
8 echo '<div id="cwpai_delivery_date_field"><h2>' . __("Delivery Date") . "</h2>";
9 woocommerce_form_field(
10 "cwpai_delivery_date",
11 [
12 "type" => "date",
13 "class" => ["cwpai-field-class form-row-wide"],
14 "required" => true, // this field is mandatory
15 "custom_attributes" => [
16 'min' => $min_date,
17 'max' => $max_date
18 ],
19 ],
20 $checkout->get_value("cwpai_delivery_date")
21 );
22 echo "</div>";
23}
24 
25// Save delivery date to the order meta
26add_action(
27 "woocommerce_checkout_update_order_meta",
28 "cwpai_save_delivery_date_to_order_meta"
29);
30function cwpai_save_delivery_date_to_order_meta($order_id)
31{
32 if (!empty($_POST["cwpai_delivery_date"])) {
33 update_post_meta(
34 $order_id,
35 "cwpai_delivery_date",
36 sanitize_text_field($_POST["cwpai_delivery_date"])
37 );
38 }
39}
40 
41// Display delivery date in the custom column
42add_action('woocommerce_admin_order_data_after_shipping_address', 'cwpai_my_orders_delivery_date_column');
43function cwpai_my_orders_delivery_date_column($order)
44{
45 $delivery_date = "<strong>Delivery Date:</strong><br>" . get_post_meta($order->get_id(), 'cwpai_delivery_date', true); // Get custom order meta
46 echo !empty($delivery_date) ? $delivery_date : 'N/A';
47}

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Sort Products By SKU

Added: 1 year ago

Last Updated: 2 weeks ago

For very rare cases, you may wish to allow customers to sort products by SKU. This use case would be useful in perhaps a B2B setting, events, or wholesaler.

WooCommerce

AI Verified

0

WooCommerce Product Price Display with Offer Validity

Added: 7 months ago

Last Updated: 7 months ago

How long is the discounted price of one of your products valid? This code checks if the product is being viewed on a single page and has a sale end date. If true, it adds a message indicating the offe...

WooCommerce

AI Verified

0

WooCommerce Show Number Of Units Sold On Product Archive

Added: 1 year ago

Last Updated: 1 year ago

Showing the number of units sold for products on the archive page is a great way to introduce Fear Of Missing Out (FOMO). It also gives your customers an indication of the most popular products in you...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

Performance

AI Verified

32

Remove Unused Javascript

Added: 1 year ago

Last Updated: 3 days ago

Remove Unused Javascript - and improve your Page Speed Insight Score

WooCommerce

Pro Verified

10

Deactivate some WooCommerce Checkout Fields

Added: 1 year ago

Last Updated: 2 weeks ago

Deactivate some WooCommerce Checkout Fields from showing

Elementor

AI Verified

6

CSS Grid Aid

Added: 8 months ago

Last Updated: 1 month ago

This can be used for any WordPress Builder to aid working with CSS Grids.