Php 在Woocommerce中将自定义字段添加为订单注释

Php 在Woocommerce中将自定义字段添加为订单注释,php,wordpress,woocommerce,orders,user-roles,Php,Wordpress,Woocommerce,Orders,User Roles,我正在尝试添加一个自定义字段jckwds\u date作为订单注释。我一辈子都搞不懂为什么这段代码在functions.php中不起作用 该代码还仅允许将注释添加到特定角色类型中 function wdm_my_custom_notes_on_single_order_page($order){ $user = wp_get_current_user(); $allowed_roles = array('eu_no_vat', 'super_wholesale_customer

我正在尝试添加一个自定义字段
jckwds\u date
作为订单注释。我一辈子都搞不懂为什么这段代码在
functions.php
中不起作用

该代码还仅允许将注释添加到特定角色类型中

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $product->get_id(), 'jckwds_date', true );

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}
基本上,我需要:

待补充:

试试这个

$order = wc_get_order(  $order_id );


$note = __("This my custom note...");


$order->add_order_note( $note );

$order->save();
试试这个

add_action('woocommerce_checkout_update_order_meta', 'checkout_field_update_order_meta');

                    function checkout_field_update_order_meta($order_id)
                    {
                        if (!empty($_POST['field_name'])) {
                            update_post_meta($order_id, 'MY Custom Field', sanitize_text_field($_POST['field_name']));
                        }
                    }

更新:

以下代码将从订单自定义字段“jckwds_date”(或签出过账字段值“jckwds_date”)添加订单注释,该注释将显示在后端,用于定义的用户角色:

add_action( 'woocommerce_checkout_update_order_meta', 'product_custom_field_to_custom_order_notes', 100, 2 );
function product_custom_field_to_custom_order_notes( $order_id, $data ){
    // HERE define allowed user roles
    $allowed_roles = array('administrator', 'super_wholesale_customer', 'wholesale_customer');

    $user_id = get_post_meta( '_customer_user', 'jckwds_date', true );
    $user = new WP_User( $user_id );

    // Exit if no matched user roles
    if( ! array_intersect( $allowed_roles, $user->roles ) ) return;

    // Get the date custom field (or checkout field)
    if( get_post_meta( $order_id, 'jckwds_date', true ) ){
        $note = get_post_meta( $order_id, 'jckwds_date', true );
    } elseif( isset( $_POST['jckwds_date'] ) ){
        $note = sanitize_text_field( $_POST['jckwds_date'] );
    }

    // The order note
    if( isset($note) && ! empty($note) ){
        $order = wc_get_order( $order_id ); // The WC_Order Object
        $order->add_order_note( $note );  // Add the note
        $order->save(); // Save the order
    }
}
代码进入活动子主题(或活动主题)的function.php文件。它应该可以工作。

试试这段代码

 add_action( 'woocommerce_thankyou', 'my_note_custom' );

    function my_note_custom( $order_id ) {

          $order = new WC_Order( $order_id );
          $note = __("This my custom note...");
          $order->add_order_note( $note );

          $order->save();

    }

发现这是一个简单的操作,将
$product
更改为
$order
,因为这是我试图检索的订单自定义字段值

完整代码如下:

function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $order->get_id(), 'jckwds_date', true );

        $note = '<b>Wholesale</b>';

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}
功能wdm\u我的\u自定义\u备注\u单订单页($order){
$user=wp_get_current_user();
$allowed_roles=数组('eu_no_vat'、'super_批发客户'、'批发客户');
if(数组相交($allowed\u roles,$user->roles)){
$value=get_post_meta($order->get_id(),'jckwds_date',true);
$note=‘批发’;
echo美元价值;
$order->add\u order\u note($value$is\u customer\u note=1);
}
}

Hi,已添加自定义字段,并在签出期间显示。这只是在客户下订单时将其添加到便笺中。这是私人便笺还是给客户的便笺?私人便笺Mate您可以从下面的url引用嘿,我已经尝试过了,我可以让它打印纯文本,但无法让它通过便笺中的自定义字段值。谢谢,我需要显示实际的自定义字段值,而不仅仅是纯文本。嗨,我看不到它在哪里更新注释?只是点菜而已。自定义字段在前端工作正常,在后端保存,只是在他们下订单后将其作为私人便笺添加。@Tommy你的意思是在客户下订单后添加自定义私人便笺吗?。它只在管理面板中可见。谢谢,不幸的是,当我刚刚测试它时它不起作用:/@Tommy你的自定义字段是产品自定义字段吗?它是订单自定义字段,基本上是他们在结账时选择的交货日期。@Tommy这不一样了……我已经更新了我的答案代码,希望它能起作用。嗨,我看不出它在哪里更新笔记?只是点菜而已。自定义字段在前端工作正常,在后端保存,只是在下订单后将其作为私人便笺添加。
function wdm_my_custom_notes_on_single_order_page($order){

    $user = wp_get_current_user();
    $allowed_roles = array('eu_no_vat', 'super_wholesale_customer', 'wholesale_customer');

    if( array_intersect($allowed_roles, $user->roles ) )  {

        $value = get_post_meta( $order->get_id(), 'jckwds_date', true );

        $note = '<b>Wholesale</b>';

        echo $value;

        $order->add_order_note( $value, $is_customer_note = 1 );

    }
}