如何从WooCommerce获取所有结帐字段?

时间:2014-11-25 15:10:10

标签: php wordpress woocommerce

我希望获得所有可用的结帐字段,包括第三方字段:

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

返回致命错误:

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

是否有正式的方式来获取它们?

编辑:

我的插件需要一个下拉字段,后端有所有可用的结帐字段。我喜欢的是这样的数组(我var_dump $checkout_fields类的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. ) ) ) 变量):

{{1}}

3 个答案:

答案 0 :(得分:5)

我面临同样的问题。这是我如何管理它。

class my_checkout_fields_class {
    var $countries;

function __construct() {

        $this->countries = new WC_Countries();

}


}

现在,您可以在此课程中分别获取结算和发货字段。

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

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

问候

答案 1 :(得分:4)

我知道我已经迟到了,但我正在寻找一个确切的功能,并且接受的回答并不能让我满意,所以我深入研究了这个问题。

问题所有者说,他需要在管理区域(在他的插件中)获取所有可用的结帐字段。

他无法做到这一点,因为那时没有结账(没有客户甚至是WooCommerce会议)。

因此,解决方案是伪造会话和客户,以便在管理区域中提供结帐(使用默认用户数据)。

/*
* 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();
});

答案 2 :(得分:0)

一个迟到的答案,但我试图找到对我有用的方法并确定了这个(虽然不完美)的解决方案:

函数 [WC_Countries::get_default_address_fields()][1] 不关心 Woocommerce 会话或结帐,因此在有条件地隐藏字段时忽略购物车中的内容(我就是这种情况)。

在没有任何参数的情况下调用 WC()->countries->get_default_address_fields() 可以获得所有计费字段(不包括自定义字段),即使字段被插件隐藏。尽管第二个参数 $type 可以是前缀 'billing_' 'shipping_'。该函数在我的会话中返回以下内容(标签和占位符的瑞典语语言环境):

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
    )
)