dou7466 2017-10-29 06:01 采纳率: 0%
浏览 25
已采纳

Woocommerce基于购物车中商品总数的累计额外费用

I am looking for some code which can make an extra charge based on the total number of items in the cart like:

  • If number of items in cart is > 6 ===> extra cost = 5
  • If number of items in cart is > 12 ==> extra cost = 10

I have tried this code, but I can't get it working for my case.

How can I make an extra cost based on total number of items in the cart?

  • 写回答

1条回答 默认 最新

  • drnzpd579935 2017-10-29 08:19
    关注

    Based on (this answer) WooCommerce Cart Quantity Base Discount, you can add a progressive fee based on total number of items:

    add_action( 'woocommerce_cart_calculate_fees','woocommerce_cart_extra_cost', 10, 1 );
    function woocommerce_cart_extra_cost( $cart_object ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )  return;
    
        $cart_item_count = $cart_object->get_cart_contents_count();
    
        // CONDITIONAL ITEMS QUANTITY FEE AMOUNT
        if( $cart_item_count < 6 )
            $fee = 0;
        elseif( $cart_item_count >= 6 && $cart_item_count < 12 )
            $fee = 5;
        elseif( $cart_item_count >= 12 )
            $fee = 10;
    
        if( $fee > 0 )
            $cart_object->add_fee( __( "Extra Cost", "woocommerce" ), $fee, true);
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导