How to get all checkout fields from WooCommerce?

前端 未结 3 1910
甜味超标
甜味超标 2021-02-10 04:13

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

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

returns a fatal err

相关标签:
3条回答
  • 2021-02-10 04:37

    A late answer but I was trying to find what worked for me and settled on this (although imperfect) solution:

    The function [WC_Countries::get_default_address_fields()][1] does not care about the Woocommerce session or checkout and therefore ignores what is in the cart for when fields are conditionally hidden (which was the case for me).

    Calling WC()->countries->get_default_address_fields() without any parameters got all billing fields (excluding custom ones) even when fields were hidden by a plugin. Although the second parameter $type can be the prefix 'billing_' 'shipping_'. The function returned the following in my session (swedish locale for label & placeholder):

    Array (
        [billing_first_name] => Array (
            [label] => Förnamn
            [required] => 1
            [class] => Array (
                [0] => form-row-first
            )
            [autocomplete] => given-name
            [priority] => 10
        )
        [billing_last_name] => Array (
            [label] => Efternamn
            [required] => 1
            [class] => Array (
                [0] => form-row-last
            )
            [autocomplete] => family-name
            [priority] => 20
        )
        [billing_country] => Array (
            [type] => country
            [label] => Land/Region
            [required] => 1
            [class] => Array (
                [0] => form-row-wide
                [1] => address-field
                [2] => update_totals_on_change
            )
            [autocomplete] => country
            [priority] => 40
        )
        [billing_address_1] => Array (
            [label] => Gatuadress
            [placeholder] => Gatunamn och nummer
            [required] => 1
            [class] => Array (
                [0] => form-row-wide
                [1] => address-field
            )
            [autocomplete] => address-line1
            [priority] => 50
        )
        [billing_city] => Array (
            [label] => Ort
            [required] => 1
            [class] => Array (
                [0] => form-row-wide
                [1] => address-field
            )
            [autocomplete] => address-level2
            [priority] => 70
        )
        [billing_state] => Array (
            [type] => state
            [label] => Stat/län
            [required] => 1
            [class] => Array (
                [0] => form-row-wide
                [1] => address-field
            )
            [validate] => Array (
                [0] => state
            )
            [autocomplete] => address-level1
            [priority] => 80
            [country_field] => billing_country
            [country] => SE
        )
        [billing_postcode] => Array (
            [label] => Postnummer
            [required] => 1
            [class] => Array (
                [0] => form-row-wide
                [1] => address-field
            )
            [validate] => Array (
                [0] => postcode
            )
            [autocomplete] => postal-code
            [priority] => 90
        )
        [billing_phone] => Array (
            [label] => Telefon
            [required] => 
        )
        [billing_email] => Array (
            [label] => E-postadress
            [required] => 1
            [type] => email
            [class] => Array (
                [0] => form-row-wide
            )
            [validate] => Array (
                [0] => email
            )
            [autocomplete] => email
            [priority] => 110
        )
    )
    
    0 讨论(0)
  • 2021-02-10 04:42

    i was facing the same issue . here's how i managed it .

    class my_checkout_fields_class {
        var $countries;
    
    function __construct() {
    
            $this->countries = new WC_Countries();
    
    }
    
    
    }
    

    Now you can get billing and shipping fields respectively 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_');
    

    regards

    0 讨论(0)
  • 2021-02-10 04:51

    I know I'm late to the party, but I was looking for an exact feature and accepted answer was not satysfying for me, so I digged into this.

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

    He cant do this stright away because there is no checkout available at that point (and no customer or even WooCommerce session).

    So the solution is to fake session and customer so checkout 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's 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();
    });
    
    0 讨论(0)
提交回复
热议问题