Validate extra fields to register form woocommerce

All my excuses in advance for my English.

I’m trying to add two extra fiels: billing_firstname and billing_lastname fiels to woocommerce registration form.
I manage show fields with no problem at all. But i have this error in validation function:

Fatal error: “Call to a member function add() on a non-object…”

in the line that i have $validation_errors->add...

Here is my validation code:

function custom_register_validate_extrafields( $username, $email,$validation_errors ) {

    if ( !isset( $_POST['billing_first_name'] ) || empty( $_POST['billing_first_name'] ) ) {
        $validation_errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );

    if ( !isset( $_POST['billing_last_name'] ) || empty( $_POST['billing_last_name'] ) ) {
        $validation_errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );

 add_action( 'woocommerce_register_post', 'custom_register_validate_extrafields', 10, 3 );

I have no idea on where is the problem. Please help me. I have wordpress 4.2.1 and Woocommerce 2.3.8

Source: wordpress

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.