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?
P粉0142937382024-01-09 18:05:49
As you can see in the code try, $email
is not part of the woocommerce_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.
// Setting global variable function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) { $GLOBALS['email_id'] = $email->id; } add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 1, 4 );
Step 2) In the hook woocommerce_order_item_meta_start
, use a global variable so that we can target certain WooCommerce email notifications
function action_woocommerce_order_item_meta_start( $item_id, $item, $order, $plain_text ) { // On email notifications for line items if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) { // Getting the email ID global variable $ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : ''; $email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : ''; // NOT empty and targeting specific email. Multiple statuses can be added, separated by a comma if ( ! empty ( $email_id ) && in_array( $email_id, array( 'new_order', 'customer_completed_order' ) ) ) { // Get meta $ot_address = get_post_meta( $item->get_product_id(), 'ot_address', true ); // OR use to get meta // $ot_address = $item->get_meta( 'ot_address' ); if ( ! empty( $ot_address ) ) { printf( '' . __( 'Terms: %s', 'woocommerce' ) . '', $ot_address ); } } } } add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );