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?
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.
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 :
You can customize this function for further requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With