在Woocommerce编辑订单页面中显示可编辑的自定义字段值 [英] Display editable custom fields values in Woocommerce edit order page

查看:52
本文介绍了在Woocommerce编辑订单页面中显示可编辑的自定义字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是开发人员,但以某种方式设法将Woocommerce自定义字段添加到结帐和订购编辑页面.有类似的问题,但我找不到正确的解决方案.

I am not a developer but somehow managed to add Woocommerce custom fields to checkout and order edit pages. There are similar questions, but I can't find the correct solution.

自定义字段在管理订单编辑页面中可见,但不显示值,并且未添加到订单电子邮件中.

The custom fields are visible in the admin order edit page but they don't display the values and are not added to order emails.

我想念什么?

请查看最后的屏幕截图.

Please see screenshot at the end.

以下是所有代码的总和:

Here is all the code a put together:

// Woocommerce - Add user custom billing fields
// =============================================================================

function add_woocommerce_admin_billing_fields($billing_fields) {
    $billing_fields['billing_birthday'] = array(
        'label' => __('Datum rojstva', 'woocommerce')
    );
    $billing_fields['billing_socialno'] = array(
        'label' => __('Davčna številka', 'woocommerce')
    );

    return $billing_fields;
}
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');

function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
    if ($type_to_load == 'billing') {
        $customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
        $customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);

    }
    return $customer_data;
}
add_filter('woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);

function add_woocommerce_billing_fields($billing_fields) {
    $billing_fields['billing_birthday'] = array(
        'type' => 'tel',
        'label' => __('Datum rojstva'),
        'value' => get_post_meta( $order->id, 'billing_birthday', true ),
        'placeholder' => __('dd/mm/yyyy', 'placeholder'),
        'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
        'class' => array('form-row-first'),
        'required' => true,
        'clear' => true

    );
    $billing_fields['billing_socialno'] = array(
        'type' => 'tel',
        'label' => __('Davčna številka'),
        'value' => get_post_meta( $order->id, 'billing_socialno', true ),
        'placeholder' => _x('8-mestna številka', 'placeholder'),
        'class' => array('form-row-last'),
        'required' => false,
        'clear' => true
    );

    return $billing_fields;
}
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields');

//Doda user meta v backend profil
function add_woocommerce_customer_meta_fields($billing_fields) {
    if (isset($billing_fields['billing']['fields'])) {
        $billing_fields['billing']['fields']['billing_birthday'] = array(
            'label' => __('Datum rojstva', 'woocommerce'),
            'description' => 'Pa kaj bo končno ratalo memo milo?'
        );
        $billing_fields['billing']['fields']['billing_socialno'] = array(
            'label' => __('Davčna številka', 'woocommerce'),
            'description' => ''
        );

    }
    return $billing_fields;
}
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');

function add_woocommerce_order_fields($address, $order) {
    $address['billing_birthday'] = $order->billing_birthday . get_post_meta($order->id, '_billing_birthday', true) ;
    $address['billing_socialno'] = $order->billing_socialno;
    return $address;
}
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);

function add_woocommerce_formatted_address_replacements($replace, $args) {
    $replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
    $replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
    return $replace;
}
add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);

function add_woocommerce_localisation_address_formats($formats) {
    $formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
    return $formats;
}
add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);

// Change field type to tel woocommerce checkout

function bbloomer_change_checkout_field_input_type() {
echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";

}

add_action( 'woocommerce_after_checkout_form', 'bbloomer_change_checkout_field_input_type'); 

订单编辑页面的屏幕截图

推荐答案

我已经测试了您的代码,但有一些小错误.您已经非常接近让它按预期工作.因此,您需要在以下代码中进行一些更改:

I have tested your code and there is just some little errors. You are very near to make it work as you expect. so you should need some changes in the following code:

1)删除了值",因为此挂钩函数可用于结帐且不需要值(当您尝试从不存在的订单中获取值时,该值会更少).

1) Removed the 'values' as this hooked function works for checkout and dont need values (and still less when you are trying to get them from a non existing order).

这将避免隐藏的错误,并且当客户在先前的购买中已经填写了这些字段时,将显示正确的值……

This will avoid hidden errors and will display the correct values when customer has already filled the fields in a previous purshase…

// Add custom Checkout billing fields
add_filter('woocommerce_billing_fields', 'add_woocommerce_billing_fields', 20, 1);
function add_woocommerce_billing_fields( $billing_fields ) {

    $billing_fields['billing_birthday'] = array(
        'type' => 'tel',
        'label' => __('Datum rojstva'),
        'placeholder' => __('dd/mm/yyyy', 'placeholder'),
        'pattern' => __('\d{1,2}/\d{1,2}/\d{4}', 'pattern' ),
        'class' => array('form-row-first'),
        'required' => true,
        'clear' => true
    );

    $billing_fields['billing_socialno'] = array(
        'type' => 'tel',
        'label' => __('Davčna številka'),
        'placeholder' => _x('8-mestna številka', 'placeholder'),
        'class' => array('form-row-last'),
        'required' => false,
        'clear' => true
    );

    return $billing_fields;
}

// Change field type to tel for woocommerce checkout
add_action( 'woocommerce_after_checkout_form', 'change_checkout_field_input_type');
function change_checkout_field_input_type() {
    echo "<script>document.getElementById('billing_postcode').type = 'tel';</script>";
    echo "<script>document.getElementById('billing_birthday').type = 'tel';</script>";
}

2)此处的键有误,因此这就是为什么在管理订单编辑页面中不显示字段值的原因.

2) The keys where wrong here, so that's why the fields values doesn't get displayed in Admin order edit pages.

它是'birthday''socialno',而不是'billing_birthday''billing_socialno'.

// Setting custom fields Keys/Labels pairs in admin edit order pages and allow edit this fields correctly.
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
    $billing_fields['birthday'] = array( 'label' => __('Datum rojstva', 'woocommerce') );
    $billing_fields['socialno'] = array( 'label' => __('Davčna številka', 'woocommerce') );

    return $billing_fields;
}

3)正确获取缺少的字段值,以显示在订单编辑页面中.

3) Get correctly the missing field values to be displayed in Order edit pages.

// 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['billing_birthday'] = get_post_meta( $order->get_id(), '_billing_birthday', true );
    $address['billing_socialno'] = get_post_meta( $order->get_id(), '_billing_socialno', true );
    return $address;
}

4)其他不变的挂钩函数:

4) Other Unchanged hooked functions:

//Doda user meta v backend profil
add_filter('woocommerce_customer_meta_fields', 'add_woocommerce_customer_meta_fields');
function add_woocommerce_customer_meta_fields($fields) {
    if (isset($fields['billing']['fields'])) {
        $fields['billing']['billing_birthday'] = array(
            'label' => __('Datum rojstva', 'woocommerce'),
            'description' => 'Pa kaj bo končno ratalo memo milo?'
        );
        $fields['billing']['billing_socialno'] = array(
            'label' => __('Davčna številka', 'woocommerce'),
            'description' => ''
        );
    }
    return $fields;
}

add_filter( 'woocommerce_found_customer_details', 'add_woocommerce_found_customer_details', 10, 3);
function add_woocommerce_found_customer_details($customer_data, $user_id, $type_to_load) {
    if ($type_to_load == 'billing') {
        $customer_data[$type_to_load . 'billing_birthday'] = get_user_meta($user_id, $type_to_load . 'billing_birthday', true);
        $customer_data[$type_to_load . 'billing_socialno'] = get_user_meta($user_id, $type_to_load . 'billing_socialno', true);

    }
    return $customer_data;
}

//add_filter('woocommerce_formatted_address_replacements', 'add_woocommerce_formatted_address_replacements', 10, 2);
function add_woocommerce_formatted_address_replacements($replace, $args) {
    $replace['{billing_birthday}'] = !empty($args['billing_birthday']) ? 'Datum rojstva' . $args['billing_birthday'] : '';
    $replace['{billing_socialno}'] = !empty($args['billing_socialno']) ? 'Davčna številka' . $args['billing_socialno'] : '';
    return $replace;
}

add_filter('woocommerce_localisation_address_formats', 'add_woocommerce_localisation_address_formats', 10, 1);
function add_woocommerce_localisation_address_formats($formats) {
    $formats['default'] = $formats['default'] . "\n{billing_birthday}\n{billing_socialno}";
    return $formats;
}

代码进入活动子主题(或活动主题)的function.php文件.

电子邮件通知-显示自定义字段(及其标签).

Email notifications - Display the custom fields (with their labels).

通过活动的子主题覆盖Woocommerce模板 emails/email-addresses.php :

Overriding Woocommerce template emails/email-addresses.php via your active child theme:

可以通过以下方式复制此模板:
plugin/woocommerce/templates/emails/email-addresses.php
yourtheme/woocommerce/emails/email-addresses.php
官方文档:> 模板结构&通过主题覆盖模板

您将在第34行之后插入 (在计费电话之后) :

You will insert after line 34 (just after the billing phone) the following:

<?php
    // Billing birthday
    $billing_birthday = get_post_meta($order->get_id(), '_billing_birthday', true );
    echo $billing_birthday ? '<br/>'.__('Datum rojstva', 'woocommerce').': '.$billing_birthday : '';

    // Billing socialno
    $billing_socialno = get_post_meta($order->get_id(), '_billing_socialno', true );
    echo $billing_socialno ? '<br/>'.__('Davčna številka', 'woocommerce').': '.$billing_socialno : '';
?>

经过测试,可以正常工作.

Tested and works.

这篇关于在Woocommerce编辑订单页面中显示可编辑的自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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