Jack Trinh
Jack Trinh

Reputation: 55

Unset some WooCommerce checkout billing fields and One Page Checkout plugin

I am using WooCommerce Subscription and One Page Checkout plugins.

I am able to unset billing first name and last name (and even set 'required' attribute to false, so both fields are successively removed from my One page checkout.

But when I fill all other fields and I place order, it displays a validation error notice: "Billing First Name and Last Name are required" and I am not really sure how to solve this problem?

Maybe it has been set again from some functions or so? How I can solve this?

Upvotes: 3

Views: 1292

Answers (3)

Jack Trinh
Jack Trinh

Reputation: 55

I have solved the issue already. The root cause is because one plugin always required Billing First Name and Last Name so I use this tip below to passby the problem:

I included this code into function.php of the theme:

add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields');
function my_woocommerce_billing_fields($fields) {
$fields['billing_first_name']['default'] = 'newSubscriber';
$fields['billing_first_name']['custom_attributes']['readonly'] = TRUE;
$fields['billing_last_name']['default'] = 'newSubscriber';
$fields['billing_last_name']['custom_attributes']['readonly'] = TRUE;
return $fields;
}

So now your first name and your last name is always set as 'newSubscriber', it will pass the validation and now you hide it from dislaying using css:

p#billing_first_name_field, p#billing_last_name_field {
display: none;

}

paste these css into appearance -> Theme-> yourtheme-> additional css and save it.

So now everything would work as charm. Hope it would help somebody struggling with the problem.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254473

To remove billing first name and last name without any issues in checkout page, try to use the following instead:

// 1. Make required fields optional
add_filter( 'woocommerce_default_address_fields', 'customize_default_address_checkout_fields', 1000 );
function customize_default_address_checkout_fields( $fields ) {
    if( is_checkout() ) {
        $fields['first_name']['required'] = $fields['last_name']['required'] = false;
    }
    return $fields;
}

// 2. Remove unneeded billing fields
add_filter( 'woocommerce_billing_fields', 'customize_billing_checkout_fields', 1000 );
function customize_billing_checkout_fields( $fields ) {
    if( is_checkout() ) {
        unset($fields['billing_first_name'], $fields['billing_last_name']);
    }
    return $fields;
}

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

It should works with One Page Checkout plugin too.

Upvotes: 1

Rahul Mohabir
Rahul Mohabir

Reputation: 41

Have you tried removing it manually via child function.php as below?

add_filter( 'woocommerce_checkout_fields' , ' custom_remove_checkout_fields ' );

function custom_remove_checkout_fields( $fields ) {
unset($fields['billing']['billing_first_name']); 
unset($fields['billing']['billing_last_name']); 
return $fields; 

}

Upvotes: 0

Related Questions