根据WooCommerce购物车中购物车项目计数显示消息 [英] Display message based on cart items count in WooCommerce cart

查看:65
本文介绍了根据WooCommerce购物车中购物车项目计数显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以 woocommerce_before_cart woocommerce_before_cart_table (如果购物车中的商品总数少于X,并显示差异).我所说的项目是指个别数量而不是产品线.

I'd like to display a message in either woocommerce_before_cart or woocommerce_before_cart_table if the total number of items in the cart is less than X, and also display the difference. By items I mean individual quantities not product lines.

如何添加一个将购物车中所有物品的数量加起来并显示一条消息的函数,如果总数少于指定数量?

How can I add a function that sums the quantities of all items in the cart and displays a message if the total is less than the specified quantity?

示例:将数字设置为30,购物车中总共包含27个商品,因此系统会显示一条消息,如果您再订购3个商品,您将获得..."等.但是,如果购物车中已有30个或更多商品,则无需显示任何消息.

Example: Set the number to 30, cart contains a total of 27 items, so a message would say 'If you order 3 more items you can get...' etc. But if the cart already has 30 or more items, then no message needs to show.

推荐答案

要根据购物车项目数在购物车页面上显示自定义消息,请使用以下命令:

To display a custom message on cart page based on number of cart items count, use the following:

// On cart page only
add_action( 'woocommerce_check_cart_items', 'custom_total_item_quantity_message' );
function custom_total_item_quantity_message() {
    $items_count = WC()->cart->get_cart_contents_count();
    $min_count   = 30;

    if( is_cart() && $items_count < $min_count ){
        wc_print_notice( sprintf( __("If you order %s more items you can get…", "woocommerce"), $min_count - $items_count ), 'notice' );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

如果使用 woocommerce_before_cart woocommerce_before_cart_table ,则更改数量或删除项目时,剩余计数将不会更新……请尝试:

If using woocommerce_before_cart or woocommerce_before_cart_table the remaining count will not be updated when changing quantities or removing items… Try:

add_action( 'woocommerce_before_cart', 'custom_total_item_quantity_message' );
function custom_total_item_quantity_message() {
    $items_count = WC()->cart->get_cart_contents_count();
    $min_count   = 30;

    if( is_cart() && $items_count < $min_count ){
        echo '<div class="woocommerce-info">';
        printf( __("If you order %s more items you can get…", "woocommerce"), $min_count - $items_count );
        echo '</div>';
    }
}

或:

add_action( 'woocommerce_before_cart_table', 'custom_total_item_quantity_message' );
function custom_total_item_quantity_message() {
    $items_count = WC()->cart->get_cart_contents_count();
    $min_count   = 30;

    if( is_cart() && $items_count < $min_count ){
        echo '<div class="woocommerce-info">';
        printf( __("If you order %s more items you can get…", "woocommerce"), $min_count - $items_count );
        echo '</div>';
    }
}

这篇关于根据WooCommerce购物车中购物车项目计数显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆