woocommerce my-account 我的帳號裡面的地址重新排序成台灣慣用格式

// 帳戶編輯地址:刪除並重新排序地址字段
add_filter(  'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {
    // 僅限於帳戶頁面
    if( ! is_account_page() ) return $fields;

    ## ---- 1.刪除'address_2'字段 ---- ##

    unset($fields['address_2']);

    ## ---- 2.排序地址字段 ---- ##

    // 在下面的數組中設置順序(排序字段)
    $sorted_fields = array('last_name','first_name','company','country','postcode','state','city','address_1');

    $new_fields = array();
    $priority = 0;

    // 重新排序結算和收件字段
    foreach($sorted_fields as $key_field){
        $priority += 10;

        if( $key_field == 'company' )
            $priority += 20; // keep space for email and phone fields

        $new_fields[$key_field] = $fields[$key_field];
        $new_fields[$key_field]['priority'] = $priority;
    }
    return $new_fields;
}

// 帳戶編輯地址:重新計費帳單電子郵件和電話字段
add_filter(  'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields( $fields ) {
    // 僅限於帳戶頁面
    if( ! is_account_page() ) return $fields;

    ## ---- 對計費電子郵件和電話字段進行排序 ---- ##

    $fields['billing_email']['priority'] = 30;
    $fields['billing_email']['class'] = array('form-row-first');
    $fields['billing_phone']['priority'] = 40;
    $fields['billing_phone']['class'] = array('form-row-last');

    return $fields;
}

// 帳戶顯示的地址:刪除'address_2'
add_filter( 'woocommerce_my_account_my_address_formatted_address' , 'my_account_address_formatted_addresses', 20, 3 );
function my_account_address_formatted_addresses( $address, $customer_id, $address_type ) {
    unset($address['address_2']); // remove Address 2

    return $address;
}

發表迴響