Home > Software engineering >  Target specific WooCommerce email notifications when using the "woocommerce_order_item_meta_sta
Target specific WooCommerce email notifications when using the "woocommerce_order_item_meta_sta

Time:06-22

I have this function which adds custom meta field to the product detail in all WooCommerce emails. But I need to show only after the order is paid (this can be also just the "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 hoped that I can nest it inside if ( $email->id == 'customer_completed_order' ) {}, so the final code will 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 it stops working after that change. Any advice?

CodePudding user response:

As you can see in your code attempt, $email is not part of the woocommerce_order_item_meta_start hook. So to target certain WooCommerce email notifications, you will need a workaround.

Step 1) creating and adding a global variable, via another hook that only applies to 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 the global variable so 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( '<div>' . __( 'Terms: %s', 'woocommerce' ) . '</div>', $ot_address );
            }
        }
    }
}
add_action( 'woocommerce_order_item_meta_start', 'action_woocommerce_order_item_meta_start', 10, 4 );
  • Related