Targeting specific WooCommerce email notifications when using the "woocommerce_order_item_meta_start" hook
P粉805922437
P粉805922437 2024-01-08 19:37:47
0
1
577

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粉805922437
P粉805922437

reply all(1)
P粉014293738

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 );
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template