How to get all validation fields from WooCommerce?

I like to get all available validation fields, including third-party ones:

$fields = WC()->checkout()->checkout_fields;

      

returns a fatal error:

Fatal error:  Call to a member function get_country() on a non-object in
../wp-content/plugins/woocommerce/includes/class-wc-checkout.php</b> on line 768

      

Is there an official way to get them?

Edit:

My plugin needs a dropdown with all validation fields available on the backend. I like to have an array like this (I class var_dump

variable ):$checkout_fields

WC_Checkout

Array
(
    [billing] => Array
        (
            [billing_country] => Array
                (
                    [type] => country
                    [label] => Country
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                            [2] => update_totals_on_change
                        )

                )

            [billing_first_name] => Array
                (
                    [label] => First Name
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-first
                        )

                )

            [billing_last_name] => Array
                (
                    [label] => Last Name
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-last
                        )

                    [clear] => 1
                )

            [billing_company] => Array
                (
                    [label] => Company Name
                    [class] => Array
                        (
                            [0] => form-row-wide
                        )

                )

            [billing_address_1] => Array
                (
                    [label] => Address
                    [placeholder] => Street address
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                )

            [billing_address_2] => Array
                (
                    [placeholder] => Apartment, suite, unit etc. (optional)
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                    [required] => 
                )

            [billing_postcode] => Array
                (
                    [label] => Postcode / Zip
                    [placeholder] => Postcode / Zip
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                    [clear] => 1
                    [validate] => Array
                        (
                            [0] => postcode
                        )

                )

            [billing_city] => Array
                (
                    [label] => Town / City
                    [placeholder] => Town / City
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                )

            [billing_state] => Array
                (
                    [type] => state
                    [label] => State / County
                    [placeholder] => State / County
                    [required] => 
                    [class] => Array
                        (
                            [0] => form-row-first
                            [1] => address-field
                        )

                    [validate] => Array
                        (
                            [0] => state
                        )

                )

            [billing_email] => Array
                (
                    [label] => Email Address
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-first
                        )

                    [validate] => Array
                        (
                            [0] => email
                        )

                )

            [billing_phone] => Array
                (
                    [label] => Phone
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-last
                        )

                    [clear] => 1
                    [validate] => Array
                        (
                            [0] => phone
                        )

                )

        )

    [shipping] => Array
        (
            [shipping_country] => Array
                (
                    [type] => country
                    [label] => Country
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                            [2] => update_totals_on_change
                        )

                )

            [shipping_first_name] => Array
                (
                    [label] => First Name
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-first
                        )

                )

            [shipping_last_name] => Array
                (
                    [label] => Last Name
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-last
                        )

                    [clear] => 1
                )

            [shipping_company] => Array
                (
                    [label] => Company Name
                    [class] => Array
                        (
                            [0] => form-row-wide
                        )

                )

            [shipping_address_1] => Array
                (
                    [label] => Address
                    [placeholder] => Street address
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                )

            [shipping_address_2] => Array
                (
                    [placeholder] => Apartment, suite, unit etc. (optional)
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                    [required] => 
                )

            [shipping_postcode] => Array
                (
                    [label] => Postcode / Zip
                    [placeholder] => Postcode / Zip
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                    [clear] => 1
                    [validate] => Array
                        (
                            [0] => postcode
                        )

                )

            [shipping_city] => Array
                (
                    [label] => Town / City
                    [placeholder] => Town / City
                    [required] => 1
                    [class] => Array
                        (
                            [0] => form-row-wide
                            [1] => address-field
                        )

                )

            [shipping_state] => Array
                (
                    [type] => state
                    [label] => State / County
                    [placeholder] => State / County
                    [required] => 
                    [class] => Array
                        (
                            [0] => form-row-first
                            [1] => address-field
                        )

                    [validate] => Array
                        (
                            [0] => state
                        )

                )

        )

    [account] => Array
        (
            [account_password] => Array
                (
                    [type] => password
                    [label] => Account password
                    [required] => 1
                    [placeholder] => Password
                )

        )

    [order] => Array
        (
            [order_comments] => Array
                (
                    [type] => textarea
                    [class] => Array
                        (
                            [0] => notes
                        )

                    [label] => Order Notes
                    [placeholder] => Notes about your order, e.g. special notes for delivery.
                )

        )

)

      

+3


source to share


2 answers


i faced the same problem. here's how i did it.

class my_checkout_fields_class {
    var $countries;

function __construct() {

        $this->countries = new WC_Countries();

}


}

      

You can now receive billing and shipping fields in this class.



$billlingfields             = $this->countries->get_address_fields( $this->countries->get_base_country(),'billing_');

$shippingfields             = $this->countries->get_address_fields( $this->countries->get_base_country(),'shipping_');

      

considers

+4


source


I know I am late to the party, but I was looking for the exact characterization and the accepted answer was not satisfying to me, so I delved into it.

As the owner of the question said, he needs to get all the validation fields available in the admin area (in his plugin).

It can't handle it because there are no validations available at that moment (and no client or even WooCommerce session).



So the solution is to spoof the session and the client, so the validation will be available in the admin area (with default user data).

/*
* WooCommerce does not load session class on backend, so we need to do this manually
*/
if ( ! class_exists( 'WC_Session' ) ) {
    include_once( WP_PLUGIN_DIR . '/woocommerce/includes/abstracts/abstract-wc-session.php' );
}

/*
* admin_init hook for testing purposes
*/
add_action('admin_init', function(){
    /*
    * First lets start the session. You cant use here WC_Session directly
    * because it an abstract class. But you can use WC_Session_Handler which
    * extends WC_Session
    */
    WC()->session = new WC_Session_Handler;

    /*
    * Next lets create a customer so we can access checkout fields
    * If you will check a constructor for WC_Customer class you will see
    * that if you will not provide user to create customer it will use some
    * default one. Magic.
    */
    WC()->customer = new WC_Customer;

    /*
    * Done. You can browse all chceckout fields (including custom ones)
    */
    var_dump(WC()->checkout->checkout_fields);
    die();
});

      

+2


source







All Articles