自动将用户自定义字段添加到订单元数据 [英] Adding user custom field to the order meta-data automatically

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

问题描述

是否可以在客户下订单时自动将客户自定义字段的值复制到订单的自定义字段?

Is it possible to automatically copy the value of a Customer's custom field to an Order's custom field when this customer places the order?

应该使用任何插件/扩展程序还是通过幕后的自定义编码来完成?

Should it be done using any plugin/extension or thru customized coding behind the scenes?

此自定义字段不需要显示在客户订单视图中.当我们通过 API 获取订单时,我们只需要它来区分订单是由消费者还是批发商下的.

This custom field does not need to be displayed on customer order view. We just need it to distinguish whether the order was placed by Consumer or Wholesale when we get it thru API.

我对这个系统完全陌生,我做了很多研究,但找不到任何方向.

I'm totally new in this system, i did a lot of research but couldn't find any direction for this.

任何建议/建议将不胜感激.

Any advice/suggestion would be very appreciated.

推荐答案

您可以使用 woocommerce_thankyou 钩子将此用户数据添加到订单元数据中:

You can use woocommerce_thankyou hook to add this user data to the order meta data:

add_action( 'woocommerce_thankyou', 'orders_from_processing_to_pending', 10, 1 );
function orders_from_processing_to_pending( $order_id ) {

    if ( ! $order_id )
        return;

    $order = wc_get_order( $order_id );
    $user_id = get_current_user_id();

    //Set HERE the meta key of your custom user field
    $user_meta_key = 'some_meta_key';

    // Get here the user custom field (meta data) value
    $user_meta_value = get_user_meta($user_id, $user_meta_key, true);


    if ( ! empty($user_meta_value) )
        update_post_meta($order_id, $user_meta_key, $user_meta_value);
    else
        return;

}

代码位于活动子主题(活动主题或任何插件文件)的 function.php 文件中.

此代码已经过测试且有效.

This code is tested and works.

之后,如果您想在管理员编辑订单后端或前端客户查看订单和电子邮件通知中显示该值,您将不得不使用更多代码和其他一些钩子......

After, if you want to display that value on admin edit order backend or in frontend customer view order and emails notifications, you will have to use some more code and some other hooks…

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

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