php-WooCommerce-感谢和“我的帐户”查看订单页面上的自定义通知
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-WooCommerce-感谢和“我的帐户”查看订单页面上的自定义通知,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3770字,纯文字阅读大概需要6分钟。
内容图文
在WooCommerce上,我为每个具有不同(整数)值的产品创建了一个自定义字段days_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>";
}
}
现在,我想在“谢谢查看顺序”页面和“我的帐户>订单>查看订单页面.
可能吗?
谢谢!
解决方法:
是的,是……但是由于相应的模板上没有可用的挂钩,因此您将不得不覆盖2个模板(为此,请阅读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)我的帐户视图订单模板:
该模板位于your_theme / woocommerce / myaccount / view-order.php中
这是此模板的摘录(其中包含函数):
<?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视图订单模板:
该模板位于your_theme / woocommerce / checkout / thankyou.php中
这是此模板的摘录(其中包含函数):
<?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
内容总结
以上是互联网集市为您收集整理的php-WooCommerce-感谢和“我的帐户”查看订单页面上的自定义通知全部内容,希望文章能够帮你解决php-WooCommerce-感谢和“我的帐户”查看订单页面上的自定义通知所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。