Displaying custom field with template action hook
-
Hi
I created a product variation input area ‘Message’ on the product page and would like it to be displayed below the order data table on the invoice and packing slip using the template action hook. Can you tell me the code to put in the template-functions.php file? I tried but the meta key for the ‘Message’ field is not clear cut. I have attached a picture of the meta keys that are displayed.
Thank you
-
Hi! You can use the WooCommerce function
wc_get_order_item_meta.
See: https://wordpress.org/support/topic/order-items-post-meta/#post-9625892You can either apply this directly in the template or use the
wpo_wcpdf_after_item_metaaction hook.Good luck!
Hi
Thanks for your reply. I am a novice and this my first WP site so I’m still trying to understand it. Once I find the meta key, can you tell me the code to put in the template-functions.php file? I tried to follow the instructions in your documentation on action hooks but wasn’t successful.
Thanks in advance for your help.
Hi @omniusa
It would become something like this:
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_custom_message', 10, 3 ); function wpo_wcpdf_show_custom_message ( $template_type, $item, $order ) { if ( $template_type == 'invoice' ) { echo wc_get_order_item_meta( $item['item_id'], 'item_meta_key', true ); } }You would place this hook in the functions.php of your child-theme. If you never worked with action hooks or functions.php please read this: How to use filters
small addition to Michaels post, your meta key appears to be
Message, so that’s what you can use in the snippet too instead ofitem_meta_key.That works! Thanks so much. Your support is greatly appreciated.
The topic ‘Displaying custom field with template action hook’ is closed to new replies.