Manuel Ragazzini
Manuel Ragazzini

Reputation: 909

How to Add automatically notes into woocommerce single order page

I would like to add automatically a "custom note" inside the single order page with some details (like categories) of the products that were ordered.

Is it possible to do with a function in function.php?

Upvotes: 5

Views: 7170

Answers (1)

Domain
Domain

Reputation: 11808

Yes this is possible. You need to add below code in current theme's functions.php file.

    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 );

Output :

enter image description here

You can customize this function for further requirements.

Upvotes: 8

Related Questions