AI Verified

Name

Order note per product.

About

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

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

copy to function.PHP

Codevault

Bobkata-Snippets-

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.

History

Last modified:

12/02/2023

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:

Found 0 vulnerabilities

Order note per product.

 
                    
1function d_extra_product_field() {
2 $value = isset( $_POST['extra_product_field'] ) ? htmlspecialchars( wp_kses_post( wp_unslash( $_POST['extra_product_field'] ) ), ENT_QUOTES, 'UTF-8' ) : '';
3 $nonce = wp_create_nonce( 'd_extra_product_field_nonce' );
4 
5 echo '<div class="extra-product-field">';
6 echo '<label for="extra_product_field">' . esc_html__( 'Add a note to the purchase:', 'domain' ) . '</label>';
7 echo '<input type="text" id="extra_product_field" name="extra_product_field" value="' . esc_attr( $value ) . '" maxlength="500" />';
8 echo '<input type="hidden" name="d_extra_product_field_nonce" value="' . esc_attr( $nonce ) . '" />';
9 echo '</div>';
10}
11add_action( 'woocommerce_before_add_to_cart_button', 'd_extra_product_field', 9 );
12 
13function d_verify_extra_product_field_nonce() {
14 if ( ! isset( $_POST['d_extra_product_field_nonce'] ) ) {
15 return;
16 }
17 
18 if ( ! wp_verify_nonce( sanitize_key( $_POST['d_extra_product_field_nonce'] ), 'd_extra_product_field_nonce' ) ) {
19 wp_die( 'Security check failed' );
20 }
21}
22add_action( 'woocommerce_add_to_cart', 'd_verify_extra_product_field_nonce' );
23 
24 
25 
26function d_add_cart_item_data( $cart_item_data, $product_id ) {
27 if ( isset( $_POST['extra_product_field'] ) && isset( $_POST['d_extra_product_field_nonce'] ) && wp_verify_nonce( $_POST['d_extra_product_field_nonce'], 'd_extra_product_field_nonce' ) ) {
28 $cart_item_data['extra_product_field'] = wp_kses_post( $_POST['extra_product_field'] );
29 }
30 return $cart_item_data;
31}
32add_filter( 'woocommerce_add_cart_item_data', 'd_add_cart_item_data', 10, 2 );
33 
34function d_get_cart_item_from_session( $cart_item, $values ) {
35 if ( isset( $values['extra_product_field'] ) ) {
36 $cart_item['extra_product_field'] = $values['extra_product_field'];
37 }
38 return $cart_item;
39}
40add_filter( 'woocommerce_get_cart_item_from_session', 'd_get_cart_item_from_session', 20, 2 );
41 
42function d_add_order_item_meta( $item_id, $values ) {
43 if ( ! empty( $values['extra_product_field'] ) ) {
44 woocommerce_add_order_item_meta( $item_id, __('Note', 'domain'), $values['extra_product_field'] );
45 }
46}
47add_action( 'woocommerce_add_order_item_meta', 'd_add_order_item_meta', 10, 2 );
48 
49 
50function display_extra_product_field_in_order_view( $item_data, $cart_item ) {
51 if( isset( $cart_item['extra_product_field'] ) ){
52 $item_data[] = array(
53 'key' => esc_html__( 'Бележка', 'woocommerce' ),
54 'value' => sanitize_text_field( $cart_item['extra_product_field'] )
55 );
56 }
57 return $item_data;
58}
59 
60add_filter( 'woocommerce_get_item_data', 'display_extra_product_field_in_order_view', 10, 2 );
61 
62// add field data in email
63function d_order_email_data( $fields ) {
64 $fields['extra_product_field'] = esc_attr( __( 'extra_product_field' ) );
65 return $fields;
66}
67add_filter('woocommerce_email_order_meta_fields', 'd_order_email_data');

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

1

WooCommerce Spend £X To Unlock Free Shipping

Added: 1 year ago

Last Updated: 1 year ago

Native WooCommerce functionality means you can achieve this by setting am minimum order amount under the free shipping method options. However, this guide goes one step forward and bypasses these opti...

WooCommerce

AI Verified

0

Remove categories from shop and other pages in Woocommerce

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

0

WooCommerce Redirect Cart to Checkout page

Added: 1 year ago

Last Updated: 1 year ago

Skip the Cart page and redirect to the Checkout page.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

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.