0

I use on WooCommerce a custom field days_manufacture for each product with different (integer) values.

Also I use this code that displays a message on emails notifications with the highest value of "days of manufacture":

add_action('woocommerce_email_before_order_table', 'days_of_manufacture_view_order_and_email', 99);
    function days_of_manufacture_view_order_and_email($order, $type='email') {
        $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
        $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
        $max_days = 0;

        // Your customized style for the email template (to adapt for your needs)
        $style = 'border:solid 2px #ededed; padding:10px; font-weight:bold;';

        // Your customized text goes in here
        $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );

        foreach( $order->get_items() as $item )
            if(get_post_meta($item['product_id'], 'days_manufacture', true) > $max_days )
                $max_days = get_post_meta($item['product_id'], 'days_manufacture', true);

        if($max_days != 0) {
            if ($max_days == 1)
                $days_txt = $day_txt;

            $output = $text . $max_days . $days_txt;

            // displayed on the email notifications
            if($type == 'email')
                echo "<div class='woocommerce-info' style='$style'>$output</div>"; // <== Customize the styles if needed
            // displayed on the woocommerce templates   
            else
                echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // displayed on the templates
        }
    }

This code works great, but now, I would like to display this message ONLY in the emails "New Order", "Order Processing", "Order Hold-on" and "Failed Order".

How can I achieve this?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Felipe Honorato
  • 159
  • 1
  • 9

1 Answers1

1

Is possible to use a condition based on order status to target only certain email notification for displaying this custom message.

Here is your changed code:

add_action('woocommerce_email_before_order_table', 'days_of_manufacture_view_order_and_email', 99);

function days_of_manufacture_view_order_and_email($order, $type='email') {

    // Defining the undesired orders status
    $not_this_statuses = array('wc-completed','wc-failed','wc-cancelled');
    if(!in_array($order->post_status, $not_this_statuses)){

        $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
        $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
        $max_days = 0;

        // Your customized style for the email template (to adapt for your needs)
        $style = 'border:solid 2px #ededed; padding:10px; font-weight:bold;';

        // Your customized text goes in here
        $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );

        foreach( $order->get_items() as $item )
            if(get_post_meta($item['product_id'], 'days_manufacture', true) > $max_days )
                $max_days = get_post_meta($item['product_id'], 'days_manufacture', true);

        if($max_days != 0) {
            if ($max_days == 1)
                $days_txt = $day_txt;

            $output = $text . $max_days . $days_txt;

            // displayed on the email notifications
            if($type == 'email')
                echo "<div class='woocommerce-info' style='$style'>$output</div>"; // <== Customize the styles if needed
            // displayed on the woocommerce templates
            else
                echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // displayed on the templates
        }

    }

}

The code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This is tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399