我正在尝试根据总订单价值在购物车上显示通知: (每消费 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
)
}
}
答案 0 :(得分:2)
这是解决方案(根据评论更新):
// 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 文件中。