1
votes

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

Also I Have this code that displays a message on cart page with the highest value of "days of manufacture":

add_action('woocommerce_before_cart', 'days_of_manufacture');
function days_of_manufacture() {
    $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
    $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
    $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
    $max_days = 0;

    foreach( WC()->cart->get_cart() as $cart_item )
        if($cart_item['days_manufacture'] > $max_days)
            $max_days = $cart_item['days_manufacture'];

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

        $output = $text . $max_days . $days_txt;
        echo "<div class='woocommerce-info'>$output</div>";
    }
}

Now I would like to display this message in emails.

How can I achieve this?

Thanks

1

1 Answers

2
votes

You can do it, changing a little bit the function of this answer to your previous question and I additionally hook this function to woocommerce_email_before_order_table hook for the orders notification emails.

So you will replace that existing function by this one. And you replace also the inserted function in both templates (see below).

This function can handle displaying your custom dynamic message in the templates (as before) and in your email notifications.

Here is the 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') {
    $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 goes in function.php file of your active child theme (or theme) or also in any plugin file.

The templates changes

Now in both templates within your active child theme (or theme) woocommerce templates folder:

You will change this line:

<?php days_of_manufacture_order_view($order); // <== inserted Here ?>

By this line:

<?php days_of_manufacture_view_and_email($order, 'template'); // <== inserted Here ?>

Now the custom notice is displayed on emails too and still works on Thankyou view order page and on

My account > Orders > view order` pages too.


References: