在 WooCommerce 管理新订单自定义字段上加载用户自定义数据 [英] Load user custom data on WooCommerce admin new order custom fields

查看:39
本文介绍了在 WooCommerce 管理新订单自定义字段上加载用户自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自Woocommerce 可编辑的自定义结帐字段和 get_formatted_shipping_address 答案代码,我已经能够使用以下代码添加 2 个新的帐单和运输字段:

//结账和我的账户(添加)单元号和楼层号add_filter( 'woocommerce_billing_fields' , 'custom_field_billing_extra' );函数 custom_field_billing_extra( $fields ) {$fields['billing_house_number'] = array('类型' =>'text',//选择字段类型//'标签' =>__('自定义标签(计费)', 'woocommerce' ),'类' =>array('form-row-first my-custom-class'),//也可以是 'form-row-first' 或 'form-row-last''必需' =>false,//可选'清除' =>true,//可选'优先级' =>61,//改变字段位置增加或减少这个值'占位符' =>__('单元号', '自定义域'),);/////////////////////////////$fields['billing_floor_number'] = array('类型' =>'text',//选择字段类型//'标签' =>__('自定义标签(计费)', 'woocommerce' ),'类' =>array('form-row-last my-custom-class'),//也可以是 'form-row-first' 或 'form-row-last''必需' =>false,//可选'清除' =>true,//可选'优先级' =>61,//改变字段位置增加或减少这个值'占位符' =>__('楼层号', '自定义域'),);返回 $fields;}add_filter( 'woocommerce_shipping_fields' , 'custom_field_shipping_extra' );函数 custom_field_shipping_extra( $fields ) {$fields['shipping_house_number'] = array('类型' =>'text',//选择字段类型//'标签' =>__('自定义标签(计费)', 'woocommerce' ),'类' =>array('form-row-first my-custom-class'),//也可以是 'form-row-first' 或 'form-row-last''必需' =>false,//可选'清除' =>true,//可选'优先级' =>61,//改变字段位置增加或减少这个值'占位符' =>__('单元号', '自定义域'),);/////////////////////////////$fields['shipping_floor_number'] = array('类型' =>'text',//选择字段类型//'标签' =>__('自定义标签(计费)', 'woocommerce' ),'类' =>array('form-row-last my-custom-class'),//也可以是 'form-row-first' 或 'form-row-last''必需' =>false,//可选'清除' =>true,//可选'优先级' =>61,//改变字段位置增加或减少这个值'占位符' =>__('楼层号', '自定义域'),);返回 $fields;}//管理员可编辑的单个订单运输和计费新字段add_filter( 'woocommerce_admin_billing_fields' , 'admin_order_page_new_fields' );add_filter( 'woocommerce_admin_shipping_fields' , 'admin_order_page_new_fields' );功能 admin_order_page_new_fields( $fields ) {//包括送货电话作为可编辑字段$fields['house_number'] = array( 'label' => __('House Number', 'custom-domain'), 'show' => '0' );$fields['floor_number'] = array( 'label' => __('floor Number', 'custom-domain'), 'show' => '0' );返回 $fields;}//管理员可编辑用户传送新字段add_filter('woocommerce_customer_meta_fields', 'user_new_field_account');函数 user_new_field_account( $fields ) {$fields['billing']['fields']['house_number'][type] = $fields['shipping']['fields']['house_number'][type] ='text';$fields['billing']['fields']['house_number'][label] = $fields['shipping']['fields']['house_number'][label] = __('House Number', '自定义域');$fields['billing']['fields']['house_number'][description] = $fields['shipping']['fields']['house_number'][description] = '房屋编号';$fields['billing']['fields']['floor_number'][type] = $fields['shipping']['fields']['floor_number'][type] ='text';$fields['billing']['fields']['floor_number'][label] = $fields['shipping']['fields']['floor_number'][label] = __('Floor_number', '自定义域');$fields['billing']['fields']['floor_number'][description] = $fields['shipping']['fields']['floor_number'][description] = '楼层号';返回 $fields;}//将自定义字段添加到电子邮件add_action('woocommerce_email_customer_details','add_house_number_field_to_emails_notifications', 19, 4 );函数 add_house_number_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {$输出 = '';$shipping_house_number = get_post_meta( $order->id, '_shipping_house_number', true );如果 ( !empty($shipping_house_number) )$output .='

'.__('房屋编号:', 'custom-domain') .'</strong><span class="text">'.$shipping_house_number .'</span></div>';回声$输出;}add_action('woocommerce_email_customer_details','add_floor_number_checkout_field_to_emails_notifications', 20, 4 );函数 add_floor_number_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {$输出 = '';$shipping_floor_number = get_post_meta( $order->id, '_shipping_floor_number', true );如果 ( !empty($shipping_floor_number) )$output .='

'.__('楼层编号:', '自定义域') .'</strong><span class="text">'.$shipping_floor_number .'</span></div>';回声$输出;}

这很好用,但是当我从后端创建手动订单时,值不会从用户数据中提取,它们始终为空.
我错过了什么?

解决方案

您的代码中有一些错误:现在可以在 Admin User 页面上正确显示和更改用户数据.

还有一些缺失的代码可以在创建手动新订单时通过 Ajax 加载额外的自定义地址字段.

我还在管理员订单可编辑地址和管理员用户仪表板上的正确位置对自定义字段进行了排序(重新排序).

所以我已经完全重新审视了你的代码.第一个函数处理加载到所有其他函数上的自定义字段设置.

代码:

//处理自定义字段设置的自定义函数函数 custom_checkout_fields_settings() {$text_domain = '自定义域';返回数组('house_number' =>__('房屋号码', $text_domain),'楼层编号' =>__('楼层号', $text_domain),);}//向 Checkout 和 My account 编辑地址添加自定义账单字段add_filter( 'woocommerce_billing_fields' , 'custom_billing_fields' );函数 custom_billing_fields( $fields ) {$settings = custom_checkout_fields_settings();//加载设置//循环设置字段foreach ( $settings as $field_key => $field_label ) {$fields['billing_'.$field_key] = 数组('类型' =>'文本','占位符' =>$field_label,'类' =>$field_key === 'house_number' ?array('form-row-first') : array('form-row-last'),'必需' =>错误的,'清除' =>真的,'优先级' =>$field_key === 'house_number' ?61:62,);}返回 $fields;}//将自定义送货字段添加到结帐和我的帐户编辑地址add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_fields' );函数 custom_shipping_fields( $fields ) {$settings = custom_checkout_fields_settings();//加载设置//循环设置字段foreach ( $settings as $field_key => $field_label ) {$fields['shipping_'.$field_key] = 数组('类型' =>'文本','占位符' =>$field_label,'类' =>$field_key === 'house_number' ?array('form-row-first') : array('form-row-last'),'必需' =>错误的,'清除' =>真的,'优先级' =>$field_key === 'house_number' ?61:62,);}返回 $fields;}//在管理单订单页面上显示可编辑的自定义字段add_filter( 'woocommerce_admin_billing_fields' , 'admin_single_order_custom_addresses_fields' );add_filter( 'woocommerce_admin_shipping_fields' , 'admin_single_order_custom_addresses_fields' );功能 admin_single_order_custom_addresses_fields( $fields ) {$settings = custom_checkout_fields_settings();//加载设置$fields_2 = array();//初始化//重新排序管理员可编辑字段(循环设置字段)foreach( $fields as $key => $field_data ) {$fields_2[$key] = $field_data;//在 'address_2' 后插入新字段if('address_2' === $key ){//循环设置字段foreach ( $settings as $field_key => $field_label ) {$fields_2[$field_key] = 数组('标签' =>$field_label,'显示' =>'0','wrapper_class' =>$field_key === 'floor_number' ?'_billing_phone_field' : '',);}}}返回 $fields_2;//返回排序后的字段}//加载 Ajax 自定义字段数据作为客户账单/送货地址字段add_filter('woocommerce_ajax_get_customer_details', 'add_custom_fields_to_ajax_customer_details', 10, 3);函数 add_custom_fields_to_ajax_customer_details( $data, $customer, $user_id ) {$settings = custom_checkout_fields_settings();//加载设置//循环设置字段foreach( $settings as $field_key => $field_label ) {$data['billing'][$field_key] = $customer->get_meta('billing_'.$field_key);$data['shipping'][$field_key] = $customer->get_meta('shipping_'.$field_key);}返回 $data;}//在管理单个用户页面上显示重新排序的可编辑自定义字段add_filter('woocommerce_customer_meta_fields', 'admin_user_custom_field');功能 admin_user_custom_field( $fields ) {$settings = custom_checkout_fields_settings();//加载设置$fields_2 = array();//初始化//重新排序管理员可编辑字段(循环退出字段)foreach( $fields as $fieldset_key => $fieldset_data ) {foreach( $fieldset_data as $fieldset_data_key => $data ) {if( $fieldset_data_key === 'title' ) {$fields_2[$fieldset_key][$fieldset_data_key] = $data;} 别的 {foreach ( $data as $key => $field ) {$fields_2[$fieldset_key][$fieldset_data_key][$key] = $field;//在address_2"之后插入新字段if( $fieldset_key . '_address_2' === $key ){//循环设置字段foreach ( $settings as $field_key => $field_label ) {$fields_2[$fieldset_key][$fieldset_data_key][$fieldset_key.'_'.$field_key] = array('标签' =>$field_label,'说明' =>'',);}}}}}}返回 $fields_2;}//在电子邮件通知中显示运输自定义字段值add_action('woocommerce_email_customer_details','display_custom_fields_on_emails_notifications', 30, 4);函数 display_custom_fields_on_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {$settings = custom_checkout_fields_settings();//加载设置//循环设置字段foreach ( $settings as $field_key => $field_label ) {$meta_value = $order->get_meta('_shipping_'.$field_key);如果(!空($meta_value)){echo '

'.$field_label .':' .'</strong><span class="text">'.$meta_value .'</span></div>';}}}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

现在,您的自定义字段值会在手动管理新订单上加载,当您设置客户时,甚至当您在现有管理订单上加载客户数据时(编辑).

Inspired by Woocommerce editable custom checkout field and in get_formatted_shipping_address answer code, I have been able to add 2 new billing and shipping fields, with the following code:

// Checkout and my account (add) unit number and floor number
add_filter( 'woocommerce_billing_fields' , 'custom_field_billing_extra' );
function custom_field_billing_extra( $fields ) {
    $fields['billing_house_number'] = array(
        'type' => 'text', // chose the field type
        //'label' =>  __('Custom label (billing)',  'woocommerce' ),
        'class' => array('form-row-first my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 61, // To change the field location increase or decrease this value
        'placeholder' => __('Unit number', 'custom-domain'),
    );
    /////////////////////////////
    
        $fields['billing_floor_number'] = array(
        'type' => 'text', // chose the field type
        //'label' =>  __('Custom label (billing)',  'woocommerce' ),
        'class' => array('form-row-last my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 61, // To change the field location increase or decrease this value
        'placeholder' => __('Floor number', 'custom-domain'),
    );
    return $fields;
}


add_filter( 'woocommerce_shipping_fields' , 'custom_field_shipping_extra' );
function custom_field_shipping_extra( $fields ) {
    $fields['shipping_house_number'] = array(
        'type' => 'text', // chose the field type
        //'label' =>  __('Custom label (billing)',  'woocommerce' ),
        'class' => array('form-row-first my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 61, // To change the field location increase or decrease this value
        'placeholder' => __('Unit number', 'custom-domain'),
    );
    /////////////////////////////
        $fields['shipping_floor_number'] = array(
        'type' => 'text', // chose the field type
        //'label' =>  __('Custom label (billing)',  'woocommerce' ),
        'class' => array('form-row-last my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 61, // To change the field location increase or decrease this value
        'placeholder' => __('Floor number', 'custom-domain'),
    );
    return $fields;
}

// Admin editable single orders shipping and billing new fields
add_filter( 'woocommerce_admin_billing_fields' , 'admin_order_page_new_fields' );
add_filter( 'woocommerce_admin_shipping_fields' , 'admin_order_page_new_fields' );
function admin_order_page_new_fields( $fields ) {
    // Include shipping phone as editable field
    $fields['house_number'] = array( 'label' => __('House Number', 'custom-domain'), 'show' => '0' );
    
    $fields['floor_number'] = array( 'label' => __('floor Number', 'custom-domain'), 'show' => '0' );

    return $fields;
}


// Admin editable User shipping new fields
add_filter( 'woocommerce_customer_meta_fields', 'user_new_field_account' );
function user_new_field_account( $fields ) {
    $fields['billing']['fields']['house_number'][type]  = $fields['shipping']['fields']['house_number'][type]  ='text';
    $fields['billing']['fields']['house_number'][label]  = $fields['shipping']['fields']['house_number'][label]  = __('House Number', 'custom-domain');
    $fields['billing']['fields']['house_number'][description]  = $fields['shipping']['fields']['house_number'][description]  = 'House Number';
    
        $fields['billing']['fields']['floor_number'][type]  = $fields['shipping']['fields']['floor_number'][type]  ='text';
    $fields['billing']['fields']['floor_number'][label]  = $fields['shipping']['fields']['floor_number'][label]  = __('Floor Number', 'custom-domain');
    $fields['billing']['fields']['floor_number'][description]  = $fields['shipping']['fields']['floor_number'][description]  = 'Floor Number';

    return $fields;
}

// add the custom fields to email
add_action('woocommerce_email_customer_details','add_house_number_field_to_emails_notifications', 19, 4 );
function add_house_number_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $shipping_house_number = get_post_meta( $order->id, '_shipping_house_number', true );

    if ( !empty($shipping_house_number) )
        $output .= '<div><strong>' . __('House Number:', 'custom-domain') . '</strong> <span class="text">' . $shipping_house_number . '</span></div>';

    echo $output;
}

add_action('woocommerce_email_customer_details','add_floor_number_checkout_field_to_emails_notifications', 20, 4 );
function add_floor_number_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $shipping_floor_number = get_post_meta( $order->id, '_shipping_floor_number', true );

    if ( !empty($shipping_floor_number) )
        $output .= '<div><strong>' . __('Floor Number:', 'custom-domain') . '</strong> <span class="text">' . $shipping_floor_number . '</span></div>';

    echo $output;
}

This is working great but when I create a manual order from the backend the values do not get pull from the user data they are always empty.
what am I missing?

解决方案

There are some mistakes in your code: Now the user data can be correctly displayed and changed on Admin User pages.

There is also some missing code to be able to Ajax load the additional custom addresses fields when creating a manual new order.

I have also sorted (reordered) the custom fields in the right location on admin order editable addresses and on admin User dashboard too.

so I have revisited completely your code. The first function handle your custom field settings that are loaded on all other functions.

The code:

// Custom function that handle the Custom field settings
function custom_checkout_fields_settings() {
    $text_domain = 'custom-domain';

    return array(
        'house_number' => __('House number', $text_domain),
        'floor_number' => __('Floor number', $text_domain),
    );
}

// Add custom billing fields to Checkout and My account edit addresses
add_filter( 'woocommerce_billing_fields' , 'custom_billing_fields' );
function custom_billing_fields( $fields ) {
    $settings = custom_checkout_fields_settings(); // Load settings

    // Loop through setting fields
    foreach ( $settings as $field_key => $field_label ) {
        $fields['billing_'.$field_key] = array(
            'type'        => 'text',
            'placeholder' => $field_label,
            'class'       => $field_key === 'house_number' ? array('form-row-first') : array('form-row-last'),
            'required'    => false,
            'clear'       => true,
            'priority'    => $field_key === 'house_number' ? 61 : 62,
        );
    }
    return $fields;
}

// Add custom shipping fields to Checkout and My account edit addresses
add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_fields' );
function custom_shipping_fields( $fields ) {
    $settings = custom_checkout_fields_settings(); // Load settings

    // Loop through setting fields
    foreach ( $settings as $field_key => $field_label ) {
        $fields['shipping_'.$field_key] = array(
            'type'        => 'text',
            'placeholder' => $field_label,
            'class'       => $field_key === 'house_number' ? array('form-row-first') : array('form-row-last'),
            'required'    => false,
            'clear'       => true,
            'priority'    => $field_key === 'house_number' ? 61 : 62,
        );
    }
    return $fields;
}

// Display editable custom fields on admin single order pages
add_filter( 'woocommerce_admin_billing_fields' , 'admin_single_order_custom_addresses_fields' );
add_filter( 'woocommerce_admin_shipping_fields' , 'admin_single_order_custom_addresses_fields' );
function admin_single_order_custom_addresses_fields( $fields ) {
    $settings = custom_checkout_fields_settings(); // Load settings
    $fields_2 = array(); // Initializing

    // Reorder admin editable fields (Loop through setting fields)
    foreach( $fields as $key => $field_data ) {
        $fields_2[$key] = $field_data;

        // Insert new fields after 'address_2'
        if( 'address_2' === $key )
        {
            // Loop through setting fields
            foreach ( $settings as $field_key => $field_label ) {
                $fields_2[$field_key] = array(
                    'label'         => $field_label,
                    'show'          => '0',
                    'wrapper_class' => $field_key === 'floor_number' ? '_billing_phone_field' : '',
                );
            }
        }
    }
    return $fields_2; // return sorted fields
}

// Load Ajax custom field data as customer billing/shipping address fields
add_filter( 'woocommerce_ajax_get_customer_details' , 'add_custom_fields_to_ajax_customer_details', 10, 3 );
function add_custom_fields_to_ajax_customer_details( $data, $customer, $user_id ) {
    $settings = custom_checkout_fields_settings(); // Load settings

    // Loop through setting fields
    foreach( $settings as $field_key => $field_label ) {
        $data['billing'][$field_key] = $customer->get_meta('billing_'.$field_key);
        $data['shipping'][$field_key] = $customer->get_meta('shipping_'.$field_key);
    }

    return $data;
}

// Display reordered editable custom fields on admin single User pages
add_filter( 'woocommerce_customer_meta_fields', 'admin_user_custom_field' );
function admin_user_custom_field( $fields ) {
    $settings = custom_checkout_fields_settings(); // Load settings
    $fields_2 = array(); // Initializing

    // Reorder admin editable fields (loop theough exiting fields)
    foreach( $fields as $fieldset_key => $fieldset_data ) {
        foreach( $fieldset_data as $fieldset_data_key => $data ) {
            if( $fieldset_data_key === 'title' ) {
                $fields_2[$fieldset_key][$fieldset_data_key] = $data;
            } else {
                foreach ( $data as $key => $field ) {
                    $fields_2[$fieldset_key][$fieldset_data_key][$key] = $field;

                    // Insert new fields after "address_2"
                    if( $fieldset_key . '_address_2' === $key )
                    {
                        // Loop through setting fields
                        foreach ( $settings as $field_key => $field_label ) {
                            $fields_2[$fieldset_key][$fieldset_data_key][$fieldset_key.'_'.$field_key] = array(
                                'label'         => $field_label,
                                'description' => '',
                            );
                        }
                    }
                }
            }
        }
    }
    return $fields_2;
}

// Display shipping custom fields values on email notifications
add_action('woocommerce_email_customer_details','display_custom_fields_on_emails_notifications', 30, 4 );
function display_custom_fields_on_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
    $settings = custom_checkout_fields_settings(); // Load settings

    // Loop through setting fields
    foreach ( $settings as $field_key => $field_label ) {
        $meta_value = $order->get_meta('_shipping_'.$field_key);
        if ( ! empty($meta_value) ) {
            echo '<div><strong>' . $field_label . ':' . '</strong> <span class="text">' . $meta_value . '</span></div>';
        }
    }
}

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

Now your custom fields values are loaded on manual admin New order, when you set a customer or even when you load customer data on existing admin orders (edit).

这篇关于在 WooCommerce 管理新订单自定义字段上加载用户自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆