php - Displaying a custom notice on Emails based on item custom field highest value -


on woocommerce have custom field days_manufacture each product different (integer) values.

also have code displays message on cart page 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 produced in: ', 'your_theme_domain_slug' );     $max_days = 0;      foreach( wc()->cart->get_cart() $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 display message in emails.

how can achieve this?

thanks

you can it, changing little bit function of this answer previous question , additionally hook function woocommerce_email_before_order_table hook orders notification emails.

so replace existing function one. , replace inserted function in both templates (see below).

this function can handle displaying custom dynamic message in templates (as before) , in email notifications.

here 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;      // customized style email template (to adapt needs)     $style = 'border:solid 2px #ededed; padding:10px; font-weight:bold;';      // customized text goes in here     $text = __('your order produced in: ', 'your_theme_domain_slug' );      foreach( $order->get_items() $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 email notifications         if($type == 'email')             echo "<div class='woocommerce-info' style='$style'>$output</div>"; // <== customize styles if needed         // displayed on woocommerce templates            else             echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // displayed on templates     } } 

this code goes in function.php file of active child theme (or theme) or in plugin file.

the templates changes

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

you change line:

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

by line:

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

now custom notice displayed on emails , still works on thankyou view order page , on

my account > orders > view order` pages too.


references:


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -