如何自动将笔记添加到woocommerce单一订单页面

时间:2015-08-27 13:19:19

标签: php function woocommerce

我想在单个订单页面中自动添加“自定义备注”,其中包含订购产品的一些详细信息(如类别)。

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

1 个答案:

答案 0 :(得分:8)

是的,这是可能的。您需要在当前主题的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 );

输出:

enter image description here

您可以自定义此功能以满足其他要求。

相关问题