dqk94069 2019-03-27 00:08
浏览 186
已采纳

将自定义结算数据添加到Woocommerce管理订单中的结算格式地址

With Woocommerce, I would like see user meta data in admin order edit pages using woocommerce_localisation_address_formats hook.

Based on "Woocommerce Edit order on admin dashboard" answer code, that I have lightly changed as follow:

add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
    if( is_admin() ){
        $address_formats = array();

        $countries = array('default', 'AU', 'AT', 'BE', 'CA', 'CH', 'CL', 'CN', 'CZ',
        'DE', 'EE', 'FI', 'DK', 'FR', 'HK', 'HU', 'IN', 'IS', 'IT', 'JP', 'TW', 'LI',
        'NL', 'NZ', 'NO', 'PL', 'PT', 'SK', 'SI', 'ES', 'SE', 'TR', 'US', 'VN' );

        foreach( $countries as $country_code ) {
            $address_formats[$country_code] = "{company}
{name}
{address_1} {address_2} {postcode}
{city} {state}
{country}
{billing_nombrecontacto}";
        }
    }
    return $address_formats;
}

Where {billing_nombrecontacto} should be custom data to insert in admin order pages… But it doesn't make anything.

To add a billing_nombrecontacto I have used the following code:

add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields,$fields2)
{
    $fields['billing_nombrecontacto'] = array(
        'label' => __('Nombre de contacto', 'woocommerce'), // Add custom field label
        'placeholder' => _x('Nombre de contacto', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')    // add class name

    );

    $fields['billing_cuit'] = array(
        'label' => __('CUIT', 'woocommerce'), // Add custom field label
        'placeholder' => _x('CUIT', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')    // add class name

    );

    $fields['billing_emaildelvendedor'] = array(
        'label' => __('Email del Contacto', 'woocommerce'), // Add custom field label
        'placeholder' => _x('email del Vendedor', 'placeholder', 'woocommerce'), // Add custom field placeholder
        'required' => false, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'text', // add field type
        'class' => array('my-css')    // add class name

    );

    $fields['billing_nombrecontacto']['priority'] = 102;

    return  $fields;
}

The data is collected correctly, I can see it in my account for this custom field.

How to make it work? I am stuck.

  • 写回答

1条回答 默认 最新

  • dongyan5815 2019-03-27 02:29
    关注

    Update: Added an additional missing function displaying the billing_nombrecontacto field value…

    I have revisited your existing code and added some more code to make it work:

    // Add / Display additional billing fields in checkout and My account > Edit adresses > Billing form
    add_filter( 'woocommerce_billing_fields', 'additional_billing_fields', 10, 1 );
    function additional_billing_fields( $fields )
    {
        $fields['billing_nombrecontacto'] = array(
            'type'        => 'text', // add field type
            'label'       => __('Nombre de contacto', 'woocommerce'), // Add custom field label
            'placeholder' => _x('Nombre de contacto', 'placeholder', 'woocommerce'), // Add custom field placeholder
            'class'       => array('my-css'), // add class name
            'required'    => false, // if field is required or not
            'clear'       => false, // add clear or not
            'priority'    => 102,    // define the priority
        );
    
        $fields['billing_cuit'] = array(
            'type'        => 'text', // add field type
            'label'       => __('CUIT', 'woocommerce'), // Add custom field label
            'placeholder' => _x('CUIT', 'placeholder', 'woocommerce'), // Add custom field placeholder
            'class'       => array('my-css'), // add class name
            'required'    => false, // if field is required or not
            'clear'       => false, // add clear or not
    
        );
    
        $fields['billing_emaildelvendedor'] = array(
            'type'        => 'text', // add field type
            'label'       => __('Email del Contacto', 'woocommerce'), // Add custom field label
            'placeholder' => _x('email del Vendedor', 'placeholder', 'woocommerce'), // Add custom field placeholder
            'class'       => array('my-css'),  // add class name
            'required'    => false, // if field is required or not
            'clear'       => false, // add clear or not
    
        );
    
        return  $fields;
    }
    
    // Adding custom placeholder to woocommerce formatted billing address only on Backend
    add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
    function admin_localisation_address_formats( $address_formats ){
        // Only in backend (Admin)
        if( is_admin() ) {
            foreach( $address_formats as $country_code => $address_format ) {
                $address_formats[$country_code] .= "
    {nombrecontacto}";
            }
        }
        return $address_formats;
    }
    
    // Custom placeholder replacement to woocommerce formatted billing address
    add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
    function custom_formatted_address_replacements( $replacements, $args  ) {
        $replacements['{nombrecontacto}'] = ! empty($args['nombrecontacto'])  ? $args['nombrecontacto'] : '';
    
        return $replacements;
    }
    
    // Get the field values to be displayed in admin Order edit pages
    add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
    function add_woocommerce_order_fields($address, $order ) {
        $address['nombrecontacto'] = $order->get_meta('_billing_nombrecontacto');
    
        return $address;
    }
    

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

    enter image description here


    Bonus - Make the field editable:

    // Make the custom billing field Editable in Admin order pages
    add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
    function add_woocommerce_admin_billing_fields($billing_fields) {
        $billing_fields['nombrecontacto'] = array( 'label' => __('Nombre contacto', 'woocommerce') );
    
        return $billing_fields;
    }
    

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题