WooCommerce - Custom notice on Thankyou and “My account” view order pages

后端 未结 1 1259
终归单人心
终归单人心 2021-01-16 02:08

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

Also I Have this code that dis

相关标签:
1条回答
  • 2021-01-16 02:33

    Yes it is… but as there is no available hooks for this on the corresponding templates, You will have to override 2 templates (for that please read how to override woocommerce templates via a theme).

    Step 1 - The 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 will be produced in: ', 'your_theme_domain_slug' );
            $max_days = 0;
    
            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;
                echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // forcing display for some themes
            }
    }
    

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

    Step 2 - inserting the function in the templates

    1) The My Account view order template:

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

    Here is an extract of this template (with the function inside it):

    <?php
    /**
     * View Order
     *
     * Shows the details of a particular order on the account page.
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/view-order.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any 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) The Thankyou view order template:

    This template is located in your_theme/woocommerce/checkout/thankyou.php

    Here is an extract of this template (with the function inside it):

    <?php
    /**
     * Thankyou page
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/checkout/thankyou.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any 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 is tested and working


    References:

    • Template Structure + Overriding Templates via a Theme
    • Woocommerce template myaccount > view-order.php
    • Woocommerce template checkout > thankyou.php
    0 讨论(0)
提交回复
热议问题