WooCommerce-有关Thankyou和“我的帐户”的自定义通知查看订单页面 [英] WooCommerce - Custom notice on Thankyou and "My account" view order pages
问题描述
在WooCommerce上,我为每个具有不同(整数)值的产品创建了一个自定义字段 days_manufacture
。
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 thankyou
view order page and in My account > Orders > View order
pages.
有可能吗?
谢谢!
推荐答案
是的……但是由于相应模板上没有可用的钩子,您将不得不覆盖2模板(为此,请阅读 如何通过主题覆盖woocommerce模板 )。
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).
步骤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
}
}
此代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中。
步骤2-插入模板中的功能
1)我的帐户视图订单模板:
1) The My Account view order template:
此模板是位于 your_theme / woocommerce / myaccount / view-order.php
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)Thankyou视图订单模板:
2) The Thankyou view order template:
此模板位于 your_theme / woocommerce / checkout / thankyou.php中
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' ) ) : ?>
此代码已经过测试并且可以正常工作
参考文献:
- Template Structure + Overriding Templates via a Theme
- Woocommerce template myaccount > view-order.php
- Woocommerce template checkout > thankyou.php
这篇关于WooCommerce-有关Thankyou和“我的帐户”的自定义通知查看订单页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!