php - WooCommerce - Custom notice on Thankyou and "My account" view order pages -


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 thankyou view order page , in my account > orders > view order pages.

is possible?

thanks!

yes is… there no available hooks on corresponding templates, have override 2 templates (for please read how override woocommerce templates via theme).

step 1 - function

function days_of_manufacture_order_view($order) {         $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( $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;             echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // forcing display themes         } } 

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

step 2 - inserting function in templates

1) account view order template:

this template located in your_theme/woocommerce/myaccount/view-order.php

here extract of template (with function inside it):

<?php /**  * view order  *  * shows details of particular order on account page.  *  * template can overridden copying yourtheme/woocommerce/myaccount/view-order.php.  *  * however, on occasion woocommerce need update template files ,  * (the theme developer) need copy new files theme  * maintain compatibility. try little possible,  * happen. when occurs version of template file bumped ,  * readme list important changes.  *  * @see     https://docs.woocommerce.com/document/template-structure/  * @author  woothemes  * @package woocommerce/templates  * @version 2.6.0  */  if ( ! defined( 'abspath' ) ) {     exit; }  ?> <?php days_of_manufacture_order_view($order); // <== inserted here ?> <p><?php  // … … … 

2) thankyou view order template:

this template located in your_theme/woocommerce/checkout/thankyou.php

here extract of template (with function inside it):

<?php /**  * thankyou page  *  * template can overridden copying yourtheme/woocommerce/checkout/thankyou.php.  *  * however, on occasion woocommerce need update template files ,  * (the theme developer) need copy new files theme  * maintain compatibility. try little possible,  * happen. when occurs version of template file bumped ,  * readme list important changes.  *  * @see         https://docs.woocommerce.com/document/template-structure/  * @author      woothemes  * @package     woocommerce/templates  * @version     2.2.0  */  if ( ! defined( 'abspath' ) ) {     exit; } if ( $order ) : ?>  <?php days_of_manufacture_order_view($order); // inserted here ?>      <?php if ( $order->has_status( 'failed' ) ) : ?> 

this code tested , working


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 -