如何自定义结帐woocommerce字段?

时间:2019-08-26 06:28:47

标签: php wordpress woocommerce

我需要更改结帐字段的排序。

添加了过滤器woocommerce_checkot_fields,但不起作用。

add_filter( 'woocommerce_checkout_fields', 'custom_order_checkout_fields' );
function custom_order_checkout_fields( $checkout_fields ) {
    $checkout_fields['billing']['billing_first_name']['priority'] = 10;
    $checkout_fields['billing']['billing_company']['priority'] = 20;
    $checkout_fields['billing']['billing_email']['priority'] = 30;
    $checkout_fields['billing']['billing_phone']['priority'] = 40;
    $checkout_fields['billing']['billing_country']['priority'] = 50;
    $checkout_fields['billing']['billing_postcode']['priority'] = 60;
    $checkout_fields['billing']['billing_city']['priority'] = 70;
    $checkout_fields['billing']['billing_address_1']['priority'] = 80;

    return $checkout_fields;
}

woocommerce_form_field_args仅将类添加到标签和输入。我需要在容器内包装必填字段。 有各种各样的插件,但是我想通过钩子了解解决方案。

2 个答案:

答案 0 :(得分:0)

您可以使用css如下所示:-

.woocommerce-billing-fields__field-wrapper {
  display: flex;
  flex-wrap: wrap;
}
.woocommerce form .form-row {
  display: inline-block;
}
.woocommerce form .form-row input.input-text {
  max-width: 252px;
}
#billing_first_name_field {
  order: 1;
}
#billing_last_name_field {
 order: 2;
}
#billing_company_field {
  order: 3;
}
#billing_country_field {
  order: 4;
}
#billing_address_1_field {
  order: 5;
}
#billing_address_2_field {
  order: 6;
  width: 100%;
}
#billing_city_field {
  order: 7;
}
#billing_postcode_field {
  order: 8;
}
#billing_state_field {
  order: 9;
  width:100%;
}
#billing_phone_field {
  order: 10;
}
#billing_email_field {
  order: 11;
}

答案 1 :(得分:0)

您的代码对我来说很好用,但是如果您要删除字段并将其设置为您的字段,则可以取消设置数组并以相同的顺序将其重新设置,请检查以下内容:

add_filter( 'woocommerce_checkout_fields', 'custom_order_checkout_fields' );
function custom_order_checkout_fields( $checkout_fields ) {
        $billing = $checkout_fields['billing'];
        unset($checkout_fields['billing']);
        $checkout_fields['billing'] = array();
        $checkout_fields['billing']['billing_first_name'] = $billing['billing_first_name'];
        $checkout_fields['billing']['billing_company'] = $billing['billing_company'];
        $checkout_fields['billing']['billing_email'] = $billing['billing_email'];
        $checkout_fields['billing']['billing_phone'] = $billing['billing_phone'];
        $checkout_fields['billing']['billing_country'] = $billing['billing_country'];
        $checkout_fields['billing']['billing_postcode'] = $billing['billing_postcode'];
        $checkout_fields['billing']['billing_city'] = $billing['billing_city'];
        $checkout_fields['billing']['billing_address_1'] = $billing['billing_address_1'];
        return $checkout_fields;
}