在Woocommerce订单编辑视图中显示自定义字段值 [英] Display a custom-field value in Woocommerce orders edit view

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

问题描述

I have a similar like in here: I don't understand why the value of this custom field is not saved. Here's my code:

add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );

function altri_campi( $fields ) {
$fields['billing']['codice_fiscale'] = array(
        'class'     => array('form-row-wide'),
        'label'     => __('Codice Fiscale', 'woocommerce'),
        'placeholder'   => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide')
    );

    return $fields;

}    

// like LoizTheAztec above
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['codice_fiscale'] ) ) {
            update_post_meta( $order_id, 'Codice Fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );
        }
    }

    // then I'm expecting that custom field value will be saved somehow, but won't

    add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_codice_fiscale', true ) . '</p>';
    }

Meta field is correctly saved and printed, but I cannot add the custom field value in the order data view.

What I am doing wrong and how to display this custom field value in the Order edit view?

Otherwise the text I expect to find after Customer's billing and shipping data is void, since I read only the html part of the latest snippet.

解决方案

Here is the correct commented and explained code:

// Creating and displaying the custom checkout field in checkout page
add_filter( 'woocommerce_checkout_fields' , 'altri_campi' );
function altri_campi( $fields ) {
    $fields['billing']['codice_fiscale'] = array(
        'class'     => array('form-row-wide'),
        'label'     => __('Codice Fiscale', 'woocommerce'),
        'placeholder'   => _x('Scrivere anche il Codice Fiscale', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide')
    );
    return $fields;
}

// Saving the custom checkout field value in the order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta', 10, 1 );
function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ( ! empty( $_POST['codice_fiscale'] ) ){
        update_post_meta( $order_id, 'codice_fiscale', sanitize_text_field( $_POST['codice_fiscale'] ) );

        // get the customer ID
        $customer_id = get_post_meta( $order_id, '_customer_user', true );

        // Update customer user data
        update_user_meta( $customer_id, 'codice_fiscale', true );
    }
}

// Displaying the custom checkout field value in the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
    $codice_fiscale = get_post_meta( $order->get_id(), 'codice_fiscale', true );
    if( ! empty( $codice_fiscale ))
        echo '<p><strong>'.__('Codice Fiscale', 'woocommerce').':</strong> ' . $codice_fiscale . '</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works. You will get in backend order edit page something like (below addresses):

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

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