简体   繁体   English

在Woocommerce编辑订单页面中显示可编辑的自定义字段值

[英]Display editable custom fields values in Woocommerce edit order page

I am not a developer but somehow managed to add Woocommerce custom fields to checkout and order edit pages. 我不是开发人员,但不知何故设法添加Woocommerce自定义字段来结帐和订购编辑页面。 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. 自定义字段在管理订单编辑页面中可见, 但它们不显示值 ,也不会添加到订单电子邮件中。

What am I missing? 我错过了什么?

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'); 

screenshot of order edit page 订单编辑页面的屏幕截图

在此输入图像描述

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) 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). 1)删除'values',因为这个hooked函数适用于checkout并且不需要值(当你试图从非现有订单中获取它们时更少)。

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) The keys where wrong here, so that's why the fields values doesn't get displayed in Admin order edit pages. 2)这里的键错误, 这就是为什么字段值不会显示在管理员订单编辑页面中。

It was 'birthday' and 'socialno' instead of 'billing_birthday' and 'billing_socialno' . 它是'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) Get correctly the missing field values to be displayed in Order edit pages. 3)正确获取要在订单编辑页面中显示的缺失字段值

// 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) Other Unchanged hooked functions: 4)其他未更改的挂钩功能:

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

Code goes in function.php file of the active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。


Email notifications - Display the custom fields (with their labels). 电子邮件通知 - 显示自定义字段(及其标签)。

Overriding Woocommerce template emails/email-addresses.php via your active child theme: 通过您的活动子主题覆盖Woocommerce模板emails/email-addresses.php

This template can be overridden by copying from: 可以通过复制来覆盖此模板:
plugin/woocommerce/templates/emails/email-addresses.php
To yourtheme/woocommerce/emails/email-addresses.php to yourtheme/woocommerce/emails/email-addresses.php ...
Official documentation: Template structure & Overriding templates via a theme 官方文档: 模板结构和通过主题覆盖模板

You will insert after line 34 (just after the billing phone) the following: 您将在第34行 (在结算电话之后)插入以下内容:

<?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. 经过测试和工作。

If you want to add your custom field, it is not about just adding new billing details, but anyway it is simple enough. 如果您想添加自定义字段,则不仅仅是添加新的结算明细,而且无论如何它都很简单。

I recommend you to use hook woocommerce_admin_order_data_after_billing_address , so it will be something like this: 我建议你使用hook woocommerce_admin_order_data_after_billing_address ,所以它会是这样的:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'mishafunction' );        
function mishafunction( $order ){        
    $x = get_post_meta( $order->get_order_number(), 'CUSTOM FIELD NAME', true );
    ?>
    <div class="address">
    <p<?php if( !$x ) echo ' class="none_set"' ?>>
        <strong>Datum rojstva:</strong>
        <?php echo ( $x ) ? $x : '' ?>
    </p>
    </div>
    <div class="edit_address"><?php
    woocommerce_wp_text_input( array(
        'id' => 'CUSTOM FIELD NAME',
        'label' => 'Datum rojstva:',
        'value' => $x,
        'wrapper_class' => 'form-field-wide'
    ) );
    ?></div><?php
}

add_action( 'woocommerce_process_shop_order_meta', 'misha_save_it' );

function misha_save_it( $order_id ){
    update_post_meta( $order_id, 'CUSTOM FIELD NAME', wc_clean( $_POST[ 'CUSTOM FIELD NAME' ] ) );
}

You can also check the complete tutorial here . 您还可以在此处查看完整的教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在订单详细信息中显示自定义WooCommerce结帐字段,并使它们可编辑 - Display custom WooCommerce checkout fields in order details and make them editable Woocommerce 自定义字段:部分值不在订单页面 - Woocommerce custom fields: some values are not in the order page 在WooCommerce收到的订单页面和电子邮件上显示订单自定义字段 - Display order custom fields on WooCommerce Order received page and emails Woocommerce function 在订单编辑页面显示此自定义字段值 - Woocommerce function to display this custom field value on the order edit page 在WooCommerce订单和电子邮件通知中显示自定义字段值 - Display custom fields values in WooCommerce order and email notification 在 WooCommerce 产品页面中显示带有标签和值的自定义字段? - Display Custom fields with labels & values in WooCommerce prodcut page? Woocommerce 管理编辑订单页面常规部分中的自定义可编辑字段 - Custom editable field in Woocommerce admin edit order pages general section 在Woocommerce 3中将产品自定义字段显示为订单商品 - Display product custom fields as order items in Woocommerce 3 在 WooCommerce 的订单编辑页面上显示自定义字段 - Show custom fields on the order editing page in WooCommerce 在Woocommerce的订单页面上显示自定义字段 - Showing custom fields on Order Page in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM