简体   繁体   中英

Make Order notes required if shipping method is local pickup in Woocommerce

In Woocommerce, I would like that checkout Order Notes field will be "required" only when local pickup is selected as a shipping method.

I've managed to change the local pickup label so that the customer is instructed to add details to this field but wondered if there was a way of not allowing them to proceed until they have added details to this section?

Any help would be very much appreciated.

The code below will make "Order notes" as a mandatory field for "Local Pickup" chosen shipping method only and will display an error notice if customer try to submit his order for this requirements:

// Validate mandatory "Order notes" field for "Local Pickup" shipping methods
add_action( 'woocommerce_checkout_process', 'local_pickup_order_comment_validation', 20 );
function local_pickup_order_comment_validation() {
    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping = explode(':', $chosen_shipping);
    if ( $chosen_shipping[0] == 'local_pickup' && empty($_POST['order_comments']) ){
        wc_add_notice( __( "You need to fill up \"Order notes\" with some details.", "woocommerce" ), 'error' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM