doujian4752 2014-11-25 07:10
浏览 92
已采纳

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

I like to get all available checkout 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 offical way to get them?

Edit:

My plugin needs a dropdown field with all available checkout fields on the backend. What I like to have is an array like this (I var_dump the $checkout_fields variable of the WC_Checkout class) :

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

        )

)

展开全部

  • 写回答

2条回答 默认 最新

  • doucepei5298 2014-12-12 15:58
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部