Php 如何在woocommerce中获取带有账单的所有字段?
我正在使用Woocommerce结帐字段编辑器,并创建了几个名为billing.*的账单字段。例如,账单街、账单镇、账单大道等,而不是预定义的账单地址1和账单地址2 但是在调用Php 如何在woocommerce中获取带有账单的所有字段?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在使用Woocommerce结帐字段编辑器,并创建了几个名为billing.*的账单字段。例如,账单街、账单镇、账单大道等,而不是预定义的账单地址1和账单地址2 但是在调用$order->get_formatted_billing_address()时我只收到账单地址1和账单地址2 在woocommerce中,是否有方法调用名为billing.*的字段的所有值 以下是我尝试过的: $order->get_billing_address() 但它不起作用 提前谢谢你 截图: 这是我得到的输出
$order->get_formatted_billing_address()时代码>我只收到账单地址1和账单地址2
在woocommerce中,是否有方法调用名为billing.*
的字段的所有值
以下是我尝试过的:
$order->get_billing_address()代码>
但它不起作用
提前谢谢你
截图:
这是我得到的输出:
同时,其他字段会显示在订单页面中,但不会显示为账单详细信息。如这里所示,您可以通过woocommerce\u Order\u get\u formatted\u billing\u address
过滤器钩子修改WC\u Order
类的get\u formatted\u billing\u address
方法的输出(如中所示)
在您的情况下,要添加自定义字段:
\u计费\u街道
\u计费\u城镇
\u计费\u大道
您可以使用以下功能:
请随意更改帐单字段的排序顺序(通过更改
$data
数组的值)
//将自定义字段添加到格式化的帐单地址
添加过滤器('woocommerce\u order\u get\u formatted\u billing\u address','add\u custom\u field\u billing\u address',10,3);
函数添加\自定义\字段\账单\地址($address,$raw\ address,$order){
$countries=新的WC_国家();
//获取国家和州代码
$billing_country=$order->get_billing_country();
$billing_state=$order->get_billing_state();
//获取国家和州的全名
$full\u country=(isset($countries->countries[$billing\u country])?$countries->countries[$billing\u country]:$billing\u country;
$full_state=($billing_country&&$billing_state&&isset($countries->states[$billing_country][$billing_state])?$countries->states[$billing_country][$billing_state]:$billing state;
$data=数组(
$order->get_billing_first_name()。'.$order->get_billing_last_name(),
$order->get_billing_company(),
$order->get_billing_address_1(),
$order->get_billing_address_2(),
$order->get_billing_postcode(),
$order->get_billing_city(),
wc_strtoupper($full_state),
$order->get_meta('u billing_street',true),
$order->get_meta('u billing_town',true),
$order->get_meta('u billing_avenue',true),
);
//从数组中删除空字段
$data=阵列过滤器($data);
//使用“
”元素作为分隔符创建帐单地址
$address=内爆(“
”,$data);
返回$address;
}
该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中
现在您可以使用$order->get_formatted_billing_address()代码>
方法获取使用自定义字段格式化的帐单地址
包括在内
这让人困惑,我需要加上街道吗?在functions.php中把这段代码放在哪里?我把这段代码放在主题的functions.php中,但它不起作用。我只在给定的代码中使用了。而在字段名称中,其不带下划线。显示时没有错误。出现疑问。如何添加自定义帐单字段?你遵循这个指南了吗?这就像Vincenzo的魅力一样。非常感谢兄弟。我正在使用WooCommerce签出字段编辑器插件。
// adds the custom fields to the formatted billing address
add_filter( 'woocommerce_order_get_formatted_billing_address', 'add_custom_field_billing_address', 10, 3 );
function add_custom_field_billing_address( $address, $raw_address, $order ) {
$countries = new WC_Countries();
// gets country and state codes
$billing_country = $order->get_billing_country();
$billing_state = $order->get_billing_state();
// gets the full names of the country and state
$full_country = ( isset( $countries->countries[ $billing_country ] ) ) ? $countries->countries[ $billing_country ] : $billing_country;
$full_state = ( $billing_country && $billing_state && isset( $countries->states[ $billing_country ][ $billing_state ] ) ) ? $countries->states[ $billing_country ][ $billing_state ] : $billing_state;
$data = array(
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
$order->get_billing_company(),
$order->get_billing_address_1(),
$order->get_billing_address_2(),
$order->get_billing_postcode(),
$order->get_billing_city(),
wc_strtoupper( $full_state ),
$order->get_meta( '_billing_street', true ),
$order->get_meta( '_billing_town', true ),
$order->get_meta( '_billing_avenue', true ),
);
// removes empty fields from the array
$data = array_filter( $data );
// create the billing address using the "<br/>" element as a separator
$address = implode( '<br/>', $data );
return $address;
}