I have this feature to add custom meta fields to product details in all WooCommerce emails. But I only need it to show up after the order has been paid for (this can also be just a "completed" email).
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 ); function email_confirmation_display_order_items( $item_id, $item, $order ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); if ( ! empty($ot_address) ) { printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address ); } } }
I wish I could nest it inside a if ( $email->id == 'customer_completed_order' ) {}
so the final code would look like this:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 ); function email_confirmation_display_order_items( $item_id, $item, $order ) { if ( $email->id == 'customer_completed_order' ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); if ( ! empty($ot_address) ) { printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $ot_address ); } } } }
But after changing it it stopped working. Any suggestions?
As you can see in the code try,
$email
is not part of thewoocommerce_order_item_meta_start
hook. So, to target certain WooCommerce email notifications, you need a workaround.Step 1) Create and add a global variable via another hook that only works for WooCommerce email notifications.
Step 2) In the hook
woocommerce_order_item_meta_start
, use a global variable so that we can target certain WooCommerce email notifications