如何在 woocommerce 单个订单页面中自动添加备注

How to Add automatically notes into woocommerce single order page

我想在单个订单页面中自动添加一个 "custom note",其中包含所订购产品的一些详细信息(如类别)。

是否可以使用 function.php 中的函数?

是的,这是可能的。您需要在当前主题的 functions.php 文件中添加以下代码。

    function wdm_my_custom_notes_on_single_order_page($order){

                $category_array=array();
                foreach( $order->get_items() as $item_id => $item ) {
                    $product_id=$item['product_id'];
                    $product_cats = wp_get_post_terms( $product_id, 'product_cat' );
                    foreach( $product_cats as $key => $value ){
                        if(!in_array($value->name,$category_array)){
                                array_push($category_array,$value->name);
                        }
                    }
                }
                $note = '<b>Categories of products in this Order : </b>'.implode(' , ',$category_array);
echo $note;
$order->add_order_note( $note );

    }

    add_action( 'woocommerce_order_details_after_order_table', 'wdm_my_custom_notes_on_single_order_page',10,1 );

输出:

您可以根据需要自定义此功能。