简体   繁体   English

WooCommerce -Thankyou 和“我的帐户”查看订单页面上的自定义通知

[英]WooCommerce - Custom notice on Thankyou and "My account" view order pages

On WooCommerce I have a custom field days_manufacture for each product with different (integer) values.在 WooCommerce 上,我为每个具有不同(整数)值的产品days_manufacture了一个自定义字段days_manufacture

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 thankyou view order page and in My account > Orders > View order pages.现在我想在thankyou查看订单页面和My account > Orders > View order页面中显示此消息。

Is it possible?是否可以?

Thanks!谢谢!

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 ).是的……但是由于相应的模板上没有可用的钩子,因此您必须覆盖 2 个模板(为此,请阅读如何通过主题覆盖 woocommerce 模板)。

Step 1 - The function步骤 1 - 函数

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.此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Step 2 - inserting the function in the templates第 2 步 - 在模板中插入函数

1) The My Account view order template: 1) 我的账户查看订单模板:

This template is located in your_theme/woocommerce/myaccount/view-order.php该模板位于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: 2)Thankyou查看订单模板:

This template is located in your_theme/woocommerce/checkout/thankyou.php该模板位于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:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 中编辑我的帐户订单视图页面 - Editing My account Order view pages in WooCommerce 在WooCommerce我的帐户查看订单页面上的订单详细信息下添加自定义文本 - Add custom text under order details on WooCommerce My account view order pages 在 Woocommerce 的我的帐户订单视图页面上添加 sku 以订购商品 - Add the sku to order items on my account order view pages in Woocommerce 在 Woocommerce 3 中的我的帐户订单查看页面上显示付款说明 - Display payment instructions on my account order view pages in Woocommerce 3 在 WooCommerce 我的帐户中更改标题自定义“查看订单”端点 - Change title custom "view-order" endpoint in WooCommerce My account woocommerce 在运输表下添加自定义字段(我的帐户订单视图/谢谢) - woocommerce Adding custom field under shipping table (my account-order view/ thank you) WooCommerce:在我的帐户页面中为自定义模板分配端点 - WooCommerce: Assigning an endpoint to a custom template in my account pages 将一些我的帐户自定义字段添加到 Woocommerce 的管理员用户页面 - Adding some my account custom fields to admin user pages in Woocommerce Woocommerce 查看订单链接无我的帐户 - Woocommerce view-order link without my-account Woocommerce 我账户中的订单查看页面显示 404 not found - Woocommerce order-view page in my account show 404 not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM