Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 864
Manuel Ragazzini Avatar asked Aug 27 '15 13:08

Manuel Ragazzini


People also ask

How do I change the order of notes in WooCommerce?

To achieve that you need to use the woocommerce_checkout_fields filter and set $fields['order']['order_comments']['label'] to the text you want. Here is the code for it. You should add it to your theme's function. php file or plugin.


Video Answer


1 Answers

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.

like image 88
Domain Avatar answered Sep 20 '22 01:09

Domain