如何从 WooCommerce 获取所有结帐字段? [英] How to get all checkout fields from WooCommerce?
本文介绍了如何从 WooCommerce 获取所有结帐字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢获取所有可用的结帐字段,包括第三方的:
I like to get all available checkout fields, including third party ones:
$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
变量的 WC_Checkout
类):
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.
)
)
)
推荐答案
我遇到了同样的问题.这是我的管理方式.
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_');
问候
这篇关于如何从 WooCommerce 获取所有结帐字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文