John Davidson

php - Target specific WooCommerce email notifications when using the "woocommerce_order_item_meta_start" hook

0 comments
Message:


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?



My Answer:
To target specific WooCommerce email notifications when using the PHP code, you can use the `woocommerce_email_actions` filter hook. This hook allows you to modify the list of email actions that WooCommerce uses to send email notifications.

Here's an example code snippet that targets specific email notifications:

php
add_filter( 'woocommerce_email_actions', 'custom_email_actions', 10, 1 );

function custom_email_actions( $email_actions ) {
// Add or remove email actions as needed
$email_actions[] = 'woocommerce_order_status_pending_to_processing_notification';
$email_actions[] = 'woocommerce_order_status_pending_to_completed_notification';

return $email_actions;
}


In this code snippet, we are adding two specific email actions (`woocommerce_order_status_pending_to_processing_notification` and `woocommerce_order_status_pending_to_completed_notification`) to the list of email actions that WooCommerce uses to send email notifications.

You can customize this code snippet to target any specific email notifications that you want to modify or target. Just add or remove email actions as needed in the `custom_email_actions` function.

Remember to add this code snippet to your theme's `functions.php` file or a custom plugin to target specific WooCommerce email notifications when using PHP code.

Rate this post

3 of 5 based on 9022 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us