gpt4 book ai didi

php - 结帐字段 - 使 billing_address_2 高于 billing_address_1

转载 作者:可可西里 更新时间:2023-11-01 01:14:44 26 4
gpt4 key购买 nike

在 WooCommerce 结帐字段中,我试图在结帐表单上将 billing_address_2 设置在 billing_address_1 之上。

所以不是:

Street Address
Apartment

我想要:

Apartment
Street Address

另请注意,我正在使用名为 Avada 的主题。

我怎样才能做到这一点?

谢谢。

最佳答案

更新(与您的评论相关)...

这里有一个附加项,它从 Address1 字段中删除“地址”文本标签并将其设置为 Address2 字段,同时使该字段(可选)成为必需字段并稍微更改占位符……我还有另一个解决方案(见下文)在代码之后)。

代码如下:

add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_order' );
function custom_billing_fields_order( $fields ) {

// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['label'] = ''; // Removing the label from Adress1
$fields['billing']['billing_address_2']['label'] = __('Address', 'theme_domain');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');

// 2. Custom ordering the billing fields array (toggling address_1 with address_2)
$custom_fields_order = array(
'billing_first_name', 'billing_last_name',
'billing_company',
'billing_email', 'billing_phone',
'billing_country',
'billing_address_2', 'billing_address_1', ## <== HERE, changed order
'billing_postcode', 'billing_city'
);

foreach($custom_fields_order as $field)
$new_ordered_fields[$field] = $fields['billing'][$field];

// Replacing original fields order by the custom one
$fields['billing'] = $new_ordered_fields;


// Returning Checkout customized billing fields order
return $fields;
}

而不是切换这 2 个字段,您可以反转字段占位符并添加(可选)需要字段 Address2,这样您就不需要重新排序字段。你可以这样做:

add_filter( 'woocommerce_checkout_fields', 'custom_billing_fields_placeholders' );
function custom_billing_fields_placeholders( $fields ) {

// 1. Customizing address_1 and address_2 fields
$fields['billing']['billing_address_1']['placeholder'] = __('Apartment, suite, unit etc...', 'woocommerce');
$fields['billing']['billing_address_2']['required'] = true; // Making Address 2 field required
$fields['billing']['billing_address_2']['placeholder'] = __('Street address', 'woocommerce');

// Returning Checkout customized billing fields
return $fields;
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

代码已经过测试并且可以工作。


相关答案:Checkout fields: Hiding and showing existing fields

官方文档:Customizing checkout fields using actions and filters

关于php - 结帐字段 - 使 billing_address_2 高于 billing_address_1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com