php - 根据 WooCommerce 上的总订单价值在购物车上显示消息
问题描述
我正在尝试根据总订单价值在购物车上显示一条通知:(每花费 28.50 欧元,客户将向医院捐赠 1 件产品)
这是我到目前为止所做的(没有成功)
function wc_donation_message() {
// Get some variables
$cart_total = (float) WC()->cart->total; // Total cart amount
// Conditional messages
if ( $cart_total >= 28.50 ) {
wc_add_notice( sprintf(
__("Thank you! You just donated 1 meal", "woocommerce"), // Text message
)
}
if ( $cart_total >= 57 ) {
wc_add_notice( sprintf(
__("Thank you! You just donated 2 meals", "woocommerce"), // Text message
)
}
}
解决方案
这是解决方案(根据评论更新):
// show a notice in the cart based on the total amount
add_action( 'woocommerce_before_cart', 'boozers_count_cart_bottles' );
function boozers_count_cart_bottles() {
// gets the total of the cart
$total = WC()->cart->total;
// set the step
$step = 28.5;
// calculate the number of meals
$meals = floor( $total / $step );
// show a different message if "$meals" is less than 1
if ( $meals < 1 ) {
wc_print_notice( 'Thank you! Your order contributed to our solidarity initiative.', 'notice' );
return;
}
if ( $meals > 1 ) {
$text = 'meals';
} else {
$text = 'meal';
}
// show the notice in the cart
wc_print_notice( sprintf( __( 'Thank you! You just donated %u %s.' ), $meals, $text ), 'notice' );
}
此时将显示消息:
该代码已经过测试并且可以工作。将其添加到活动主题的 functions.php 文件中。
推荐阅读
- python - 删除 json 对象,如果值匹配
- python - 使用 USB 将两个 BBB 连接到一台 PC
- python - 使用部分列名的 Pandas 数据框查找
- reactjs - REACT CLIENT:如何获取用户输入的任意域的 IP 地址?
- node.js - 如何生成持续时间数组
- angular - Angular 6:由外部自定义元素创建元素时未构造元素选择器的指令
- ruby - Gem therubyracer 未安装在 alpine docker 容器中
- css - 在css中创建简单的slideDown和slideUp动画
- recaptcha - 谷歌recaptchas中的未知参数
- next.js - Framer-motion导入中断页面Next Js