【问题标题】:Customizing my-account addresses fields in Woocommerce 3在 Woocommerce 3 中自定义我的帐户地址字段
【发布时间】:2018-04-04 01:24:59
【问题描述】:

我想删除模板 form-edit-addresses.php 中的表单字段 billing_adress_2。

是否可以像这样重新排序表单字段:


first_name - last_name

电子邮件 - 电话

公司

地址_1

邮编

城市

状态


此更改我只想将其应用于页面(模板)form-edit-addresses.php

感谢任何帮助

【问题讨论】:

    标签: php wordpress woocommerce field hook-woocommerce


    【解决方案1】:

    在我的帐户 > 地址部分,以下挂钩函数将:

    • 删除“地址 2”帐单和送货字段
    • 重新订购帐单和送货地址字段
    • 重新排序结算电子邮件和电话字段(在名字和姓氏之后)
    • 从显示中删除“地址 2”

    您忘记了可以在 $sorted_fields 数组中轻松重新排序的 "country" 字段……

    代码:

    // 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;
    }
    

    代码在您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    如果您想在结帐页面中也有效,则必须删除以下行:

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

    在每个函数中(2次)

    【讨论】:

    • @Dom 抱歉,我没听懂?有 2 个功能协同工作进行排序……因为电子邮件和电话实际上不是地址字段而是帐户字段,因此它们在第二个功能中进行排序……然后按照说明和您想要的方式对自己进行排序,您可以轻松地对项目进行排序在数组$sorted_fields ...排序是具有“优先级”属性的女仆...
    • 这个功能一如既往的完美,现在我正在尝试修改它以适应我的需要,我会尽快让你知道.......谢谢
    • 完美!非常感谢。
    【解决方案2】:

    此代码将更改帐单地址中字段的顺序,使其显示为您想要的。将此代码放在您的主题 functions.php 文件中。

    add_filter('woocommerce_address_to_edit', 'reorder_woocommerce_address_fields', 10, 2);
    
    function reorder_woocommerce_address_fields( $address, $load_address) {
        $new_address['billing_first_name'] = $address['billing_first_name'];
        $new_address['billing_last_name'] = $address['billing_last_name'];
        $new_address['billing_email'] = $address['billing_email'];
        $new_address['billing_phone'] = $address['billing_phone'];
    
        $new_address['billing_email'] = array(
            'label'     => __('Email', 'woocommerce'),
            'required'  => true,
            'class'     => array('form-row-first'),
        );
    
        $new_address['billing_phone'] = array(
            'label'     => __('Phone', 'woocommerce'),
            'required'  => true,
            'class'     => array('form-row-last'),
            'clear'     => true
        );
    
        $new_address['billing_company'] = $address['billing_company'];
        $new_address['billing_address_1'] = $address['billing_address_1'];
        $new_address['billing_postcode'] = $address['billing_postcode'];
        $new_address['billing_city'] = $address['billing_city'];
        $new_address['billing_state'] = $address['billing_state'];
    
        return $new_address;
    }
    

    【讨论】:

    • 好的!谢谢。我正在寻找保持空间字段类
    • 这个功能很完美,正是我想要的。非常感谢...... :)
    • @Dom 谢谢队友。您能否为我的答案投票并接受,将不胜感激:)
    猜你喜欢
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2018-11-02
    • 2016-04-22
    • 2019-01-16
    • 1970-01-01
    相关资源
    最近更新 更多