Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make checkout fields required in Woocommerce checkout

For some reason, all the fields in Billing Address are marked as optional - customers are leaving the billing address fields blank and then their payments are being rejected (by Square, who is our payment processor).

I cannot find anywhere to make these fields required, and cannot figure out why they would be marked as optional in any case.

Can someone point me in the right direction?

UPDATE

I've even tried the following:

add_filter('woocommerce_billing_fields', 'force_billing_fields', 1000, 1);
function force_billing_fields($fields) {
  $fields['billing_first_name']['required'] = true;
  $fields['billing_last_name']['required'] = true;
  $fields['billing_address_1']['required'] = true;
  $fields['billing_city']['required'] = true;
  $fields['billing_postcode']['required'] = true;
  $fields['billing_country']['required'] = true;
  $fields['billing_state']['required'] = true;
  $fields['billing_email']['required'] = true;
  $fields['billing_phone']['required'] = true;

  return $fields;
}

And they're still marked as optional, except the billing phone and country are now marked as required. But the rest are still optional.

like image 214
daniel0mullins Avatar asked Jul 12 '18 16:07

daniel0mullins


1 Answers

What you can do if you don't find the guilty as explained on my comment is to use the following (using here a highest hook priority if some other code is already using those hooks):

add_filter( 'woocommerce_default_address_fields', 'customising_checkout_fields', 1000, 1 );
function customising_checkout_fields( $address_fields ) {
    $address_fields['first_name']['required'] = true;
    $address_fields['last_name']['required'] = true;
    $address_fields['company']['required'] = true;
    $address_fields['country']['required'] = true;
    $address_fields['city']['required'] = true;
    $address_fields['state']['required'] = true;
    $address_fields['postcode']['required'] = true;

    return $address_fields;
}

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

For billing phone and email you can try

add_filter('woocommerce_billing_fields', 'custom_billing_fields', 1000, 1);
function custom_billing_fields( $fields ) {
    $fields['billing_email']['required'] = true;
    $fields['billing_phone']['required'] = true;

    return $fields;
}

or

add_filter('woocommerce_checkout_fields', 'custom_billing_fields', 1000, 1);
function custom_billing_fields( $fields ) {
    $fields['billing']['billing_email']['required'] = true;
    $fields['billing']['billing_phone']['required'] = true;

    return $fields;
}
like image 144
LoicTheAztec Avatar answered Oct 12 '22 02:10

LoicTheAztec