在WooCommerce上,我为每个具有不同(整数)值的产品提供了自定义字段 days_manufacture .
此外,我有此代码在购物车页面上显示最高值为 "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>";
}
}
现在我想在 thankyou 视图顺序页面和 My account > Orders > View order 页面中显示此消息 .
可能吗?
谢谢!
1 回答
是的它是......但由于相应的模板上没有可用的钩子,你必须覆盖2个模板(为此请阅读how to override woocommerce templates via a theme) .
Step 1 - The function
此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中 .
Step 2 - inserting the function in the templates
1)我的帐户视图订单模板:
此模板位于 your_theme/woocommerce/myaccount/view-order.php
这是该模板的摘录(其中包含函数):
2)Thankyou视图订单模板:
这个模板位于 your_theme/woocommerce/checkout/thankyou.php
这是该模板的摘录(其中包含函数):
This code is tested and working
参考文献:
Template Structure + Overriding Templates via a Theme
Woocommerce template myaccount > view-order.php
Woocommerce template checkout > thankyou.php