3

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.

aynber
  • 22,380
  • 8
  • 50
  • 63
Mike Devitt
  • 115
  • 1
  • 6

1 Answers1

2

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() {
    // Convert the array to a comma separated string of values
    $chosen_shipping_methods =  implode( ',', WC()->session->get('chosen_shipping_methods') );

    // Targeting any "local_pickup" shipping method
    if ( false !== strpos( $chosen_shipping_methods, 'local_pickup' ) && empty($_POST['order_comments']) ){
        wc_add_notice( __( "You need to fill up \"Order notes\" with some details.", "woocommerce" ), 'error' );
    }
}

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

Related: Make Checkout Order notes field required for shipping method ID in WooCommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks Loic (again) for your help here. The code works but does not display the error notice, but does highlight all the existing required fields with a green border except the order comments section. – Mike Devitt Mar 28 '18 at 15:32
  • Ok, yes, I can see that it works on your test install and I've just worked out what the problem is. Thanks Loic – Mike Devitt Mar 28 '18 at 16:05