在结帐页面上更改计费字段的顺序

时间:2019-12-23 09:26:14

标签: woocommerce

我正在使用WC过滤器来订购帐单字段

过滤器似乎工作正常,但由于未知原因,它会立即恢复为默认顺序。

add_filter("woocommerce_checkout_fields", "custom_order_fields");

function custom_order_fields($fields) {
    $order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_country",
        "billing_address_1", 
        "billing_address_2",  
        "billing_state",
        "billing_city",
        "billing_phone",
        "billing_email" 
    );

    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }

    $fields["billing"] = $ordered_fields;

    $fields['billing_first_name']['priority'] = 10;
    $fields['billing_last_name']['priority'] = 20;
    $fields['billing_country']['priority'] = 30;
    $fields['billing_address_1']['priority'] = 40;
    $fields['billing_address_2']['priority'] = 50;
    $fields['billing_state']['priority'] = 40;
    $fields['billing_city']['priority'] = 70;
    $fields['billing_phone']['priority'] = 80;
    $fields['billing_email']['priority'] = 90;

    return $fields;
}

1 个答案:

答案 0 :(得分:0)

这个怎么样?

function custom_order_fields($fields) {
    $order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_country",
        "billing_address_1", 
        "billing_address_2",  
        "billing_state",
        "billing_city",
        "billing_phone",
        "billing_email" 
    );

    foreach($order as $key => $field)
    {
        if (!empty($fields["billing"][$field]))
        $fields["billing"][$field]['priority'] = ($key + 1) * 10;
    }

    return $fields;
}

另一种解决方案是像这样构建数组$order

function custom_order_fields($fields) {
    $order = array(
        "billing_first_name" => 10, 
        "billing_last_name" => 20, 
        "billing_country" => 30,
        "billing_address_1" => 40, 
        "billing_address_2" => 50,  
        "billing_state" => 60,
        "billing_city" => 70,
        "billing_phone" => 80,
        "billing_email" => 90 
    );

    foreach($order as $field => $priority)
    {
        if (!empty($fields["billing"][$field])) {
            $fields["billing"][$field]['priority'] = $priority;
        }
    }

    return $fields;
}