drcj64241 2018-04-03 17:24
浏览 119
已采纳

在Woocommerce 3中自定义我的帐户地址字段

I would like to remove the form field billing_adress_2 within the template form-edit-addresses.php.

To finish is it possible to reorder the form fields likes this:


first_name - last_name

email - phone

company

address_1

postcode

city

state


This change I would like to apply it only on the page (templates) form-edit-addresses.php

Any help is appreciated

  • 写回答

2条回答 默认 最新

  • doulan1073 2018-04-03 18:46
    关注

    In My account > Adresses section, the below hooked functions will:

    • remove "Address 2" billing and shipping fields
    • reorder billing and shipping address fields
    • reorder billing Email and phone fields (after first and last names)
    • remove "Address 2" from display

    You forgot the "country" field that you can reorder easily in the $sorted_fields array…

    The code:

    // Account Edit Adresses: Remove and reorder addresses fields
    add_filter(  'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
    function custom_default_address_fields( $fields ) {
        // Only on account pages
        if( ! is_account_page() ) return $fields;
    
        ## ---- 1.  Remove 'address_2' field ---- ##
    
        unset($fields['address_2']);
    
        ## ---- 2.  Sort Address fields ---- ##
    
        // Set the order (sorting fields) in the array below
        $sorted_fields = array('first_name','last_name','company','address_1','country','postcode','city','state');
    
        $new_fields = array();
        $priority = 0;
    
        // Reordering billing and shipping fields
        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;
    }
    
    // Account Edit Adresses: Reorder billing email and phone fields
    add_filter(  'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
    function custom_billing_fields( $fields ) {
        // Only on account pages
        if( ! is_account_page() ) return $fields;
    
        ## ---- 2.  Sort billing email and phone 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;
    }
    
    // Account Displayed Addresses : Remove '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;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and work.

    If you want to make that effective in checkout page too, you will have to remove this lines:

    // Only on account pages
    if( ! is_account_page() ) return $fields;
    

    In each function (2 times)

    enter image description here

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 怎么实现数组的循环累加,simulink
  • ¥15 51单片机最小开发板系统,想让寻迹小车在全检测到黑线(寻迹模块代码在第一块板子上)时蜂鸣器响(在第二块板子上)
  • ¥15 pbootcms多选调用成列表
  • ¥15 51单片机oled显示时钟
  • ¥15 小规模TSP问题的动态规划求解
  • ¥25 kubelet.service: Failed with result 'exit-code'.
  • ¥15 bitvise黑框内一键部署v2ray提示账户没有root怎么解决
  • ¥15 车型识别以及相似度匹配中细节特征提取以及图像模糊问题
  • ¥15 怎么用鸿蒙的ArkTs写出来啊
  • ¥30 websocket服务端多线程通信
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部