根据 WooCommerce 购物车中的购物车商品数量显示消息

时间:2023-01-17
本文介绍了根据 WooCommerce 购物车中的购物车商品数量显示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在 woocommerce_before_cartwoocommerce_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_cartwoocommerce_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 购物车中的购物车商品数量显示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Woocommerce 变体下拉选项标签添加一些数据集 下一篇:Woocommerce 结帐页面上的额外贝宝费用

相关文章

最新文章