没有挂钩woocommerce_form_field
, 有钩子woocommerce_form_field_{$args[type]}
(doc).
$args[type]
可以(看here 对于可用选项):
文本,复选框,国家,国家下面的代码将“billing\\u first\\u name”和“billing\\u last\\u name”字段包装为<div class="first-and-second-field-wrapper"> [...] </div>
.
function change_woocommerce_field_markup($field, $key, $args, $value) {
$field = \'<div class="single-field-wrapper">\'.$field.\'</div>\';
if($key === \'billing_first_name\')
$field = \'<div class="first-and-second-field-wrapper">\'.$field;
else if ($key === \'billing_last_name\')
$field = $field.\'</div>\';
return $field;
}
add_filter("woocommerce_form_field_text","change_woocommerce_field_markup", 10, 4);
它还将包装
text
使用键入字段
<div class="single-field-wrapper">...</div>
.
BUT某些具有自己类型(如state或email)的文本字段需要其他挂钩,例如:
add_filter("woocommerce_form_field_country","change_woocommerce_field_markup", 10, 4);
add_filter("woocommerce_form_field_email","change_woocommerce_field_markup", 10, 4);
<小时>
UPDATE #1 上述代码在WC-v3.3.7中工作。
在WC-v3.4中。xx您可以得到:
<div class="first-and-second-field-wrapper">
<div class="single-field-wrapper">
<div class="single-field-wrapper">
[here there are all the fields, one under the other ]
</div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
<div class="single-field-wrapper"></div>
....
<div class="single-field-wrapper"></div>
</div>
因为javascript对表单行进行排序
.woocommerce-billing-fields__field-wrapper
. 查看文件
woocommerce/assets/js/frontend/address-i18n.js
, 来自第99行
first 项目(用类标记.form-row
), 按优先级排序项目并将其插入previously selected parent element. 对于测试,更改文件address-i18.js
, 第99行
var fieldsets = $(\'.woocommerce-billing-fields__field-wrapper, ...
至
var fieldsets = $(\'.woocommerce-billing-fields__field-wrapper2, ...
并上载为address-i18.min.js
.
Removing JS sorting is not a solution, it is just a test. 不按JS排序,您将得到以下结果:
<div class="first-and-second-field-wrapper">
<div class="single-field-wrapper">
<p class="form-row ..." id="billing_first_name_field"> <label for="billing_first_name"> ... </p>
</div>
<div class="single-field-wrapper">
<p class="form-row ..." id="billing_last_name_field"> <label for="billing_last_name">... </p>
</div>
</div>
<div class="single-field-wrapper">
<p class="form-row ..." id="billing_company_field"> <label for="billing_company">... </p>
</div>