简体   繁体   English

基于 WooCommerce 中购物车项目数量的附加价格

[英]Additional price based on cart item count in WooCommerce

Based on " woocommerce change price in checkout and cart page " answer code that change the total price in checkout page, I have added some extra code to count the products that user have in cart and if user had like 9 products in cart then add some price to total:基于结帐和购物车页面中的 woocommerce 更改价格更改结帐页面中总价的答案代码,我添加了一些额外的代码来计算用户在购物车中拥有的产品,如果用户在购物车中有 9 个产品,则添加一些总价:

add_action( 'woocommerce_before_cart_totals', 'custom_cart_total' , 'get_cart_contents_count');
function custom_cart_total() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    if (WC()->cart->get_cart_contents_count() == 9){
        WC()->cart->total += 15;
    }
    elseif(WC()->cart->get_cart_contents_count() == 6){
       WC()->cart->total += 14; 
    }
    elseif(WC()->cart->get_cart_contents_count() == 4){
       WC()->cart->total += 13; 
    }

}

But it doesn't work.但它不起作用。 This image will explain everything:这张图片将解释一切:

图片

I will appreciate if anyone could correct the code and tell me how can I display the message like in the picture如果有人可以更正代码并告诉我如何显示图片中的消息,我将不胜感激

You should better use the FEE API instead, this way:你应该更好地使用 FEE API,这样:

// Add a custom packing fee based on item count
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fee', 10, 1 );
function custom_packing_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
        return;

    $count = $cart->get_cart_contents_count();

    if ( $count >= 9 ){
        $fee = 15;
    }
    elseif( $count >= 6 && $count < 9 ){
        $fee = 14;
    }
    elseif( $count >= 4 && $count < 6 ){
        $fee = 13;
    }

    if ( isset($fee) && $fee > 0 ) {
        $label = sprintf( __('Box fee (%d items)'), $count);
        $cart->add_fee( $label, $fee, false );
    }
}

Code goes in functions.php file of your active child theme (or active theme) .代码位于您的活动子主题(或活动主题)的functions.php 文件中。 Tested and works.测试和工作。

If you want to enable taxes for the packing fee, change the third argument from false to true .如果要为包装费启用税,请将第三个参数从false更改为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Woocommerce中购物车的数量增加产品价格的额外成本 - Add an extra cost to product price based on cart item count in Woocommerce 基于购物车项目数量的 WooCommerce 运费 - WooCommerce shipping cost based on cart item count Woocommerce中基于GET参数的新购物车商品价格 - New cart item price based on a GET parameter in Woocommerce 根据Woocommerce中的用户输入自定义购物车商品价格 - Custom cart item price based on user input in Woocommerce 根据自定义字段和数量阈值更改 WooCommerce 购物车商品价格 - Change WooCommerce cart item price based on a custom field and quantity threshold 购物车商品价格计算,基于Woocommerce中选择的“天”自定义字段 - Cart item price calculation, based on chosen “days” custom field in Woocommerce 根据Woocommerce中不同的基本价格计算购物车商品价格 - Cart item price calculation, based on different basic prices in Woocommerce 基于Woocommerce中的维度自定义字段的自定义购物车商品价格计算 - Custom cart item price calculation based on dimentions custom fields in Woocommerce WooCommerce 结账费用基于购物车商品数量(不包括类别) - WooCommerce checkout fee based on cart item count with category excluded 基于 Woocommerce 中购物车项目数的有条件累进百分比折扣 - Conditional progressive percentage discount based on cart item count in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM