更改本地的 WooCommerce 类
这种方法对我有效。
实际上,WooCommerce 使用一个类:form-row-first(CSS:float left)和 form-row-last:float right。
如果您将优先级从2更改为1,但ccs类是form-row-last,则该字段仍将位于右侧。
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields',1 );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_email']['priority'] = 3;
$fields['billing']['billing_email']['label'] = mb_strtoupper("adresse email");
$fields['billing']['billing_email']['class'] = array('form-left');
$fields['billing']['billing_company']['priority'] = 5;
$fields['billing']['billing_company']['label'] = mb_strtoupper("entreprise");
$fields['billing']['billing_company']['required'] = false;
$fields['billing']['billing_company']['class'] = array('form-left');
$fields['billing']['billing_phone']['priority'] = 6;
$fields['billing']['billing_phone']['label'] = mb_strtoupper("téléphone");
$fields['billing']['billing_phone']['required'] = false;
$fields['billing']['billing_phone']['class'] = array('form-right');
$fields['billing']['billing_phone']['validate'] = array('phone');
$fields['shipping']['shipping_company']['priority'] = 3;
$fields['shipping']['shipping_company']['label'] = mb_strtoupper("entreprise");
$fields['shipping']['shipping_company']['required'] = false;
$fields['shipping']['shipping_company']['class'] = array('form-left');
return $fields;
}
其他字段如first_name,last_name,address_1,address-2,post_code,city,country,state必须放置在此过滤器中。
add_filter( 'woocommerce_default_address_fields','custom_override_default_locale_fields');
function custom_override_default_locale_fields( $fields ) {
$fields['last_name']['priority'] = 1;
$fields['last_name']['label'] = mb_strtoupper("nom");
$fields['last_name']['class'] = array('form-left');
$fields['first_name']['priority'] = 2;
$fields['first_name']['label'] = mb_strtoupper("prénom");
$fields['first_name']['class'] = array('form-right');
$fields['address_1']['priority'] = 110;
$fields['address_1']['label'] = mb_strtoupper("adresse 1");
$fields['address_2']['priority'] = 120;
$fields['address_2']['label'] = mb_strtoupper("adresse 2");
$fields['postcode']['label'] = mb_strtoupper("code postal");
$fields['postcode']['priority'] = 130;
$fields['postcode']['class'] = array('form-left-small');
$fields['city']['label'] = mb_strtoupper("ville");
$fields['city']['priority'] = 140;
$fields['city']['class'] = array('form-right-big');
$fields['country']['priority'] = 4;
$fields['country']['label'] = mb_strtoupper("pays");
$fields['country']['required'] = true;
$fields['country']['class'] = array('form-right','address-field','update_totals_on_change');
$fields['state']['priority'] = 120;
$fields['state']['label'] = mb_strtoupper("téléphone");
$fields['state']['required'] = true;
$fields['state']['class'] = array('form-right');
return $fields;
return $fields;
}
以及CSS
.woocommerce form .form-right,
.woocommerce form .form-left,
.woocommerce form .form-left-small,
.woocommerce form .form-right-big {
display: inline-block;
}
.woocommerce form .form-right,
.woocommerce form .form-left {
width: 48%
}
.woocommerce form .form-right,
.woocommerce form .form-big-right {
margin-left: 2% !important;
}
.woocommerce form .form-left,
.woocommerce form .form-left-small {
margin-right: 2% !important;
}
.woocommerce form .form-big-right {
width: 63%;
}
.woocommerce form .form-left-small {
width: 33%;
}
.woocommerce form .woocommerce-input-wrapper strong {
height: 38px;
line-height: 38px;
}
结果:
![enter image description here](https://istack.dev59.com/diELR.webp)
add_filter("woocommerce_checkout_fields", "custom_override_checkout_fields", 999);
这样的值,以便它在任何潜在的重复函数之后运行。 - Frits