本文介绍了Woocommerce基于购物车中物品总数的累进额外费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一些可以根据购物车中的物品总数收取额外费用的代码,例如:

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

  • 如果购物车中的物品数> 6 ===>额外费用= 5
  • 如果购物车中的项目数> 12 ==>额外费用= 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?

推荐答案

基于(此答案) ,您可以根据商品总数添加累进费用:

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 ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )  return;

    $cart_item_count = $cart->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->add_fee( __( "Extra Cost", "woocommerce" ), $fee, true);
}

代码进入活动子主题(或主题)的functions.php文件或任何插件文件中.

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

经过测试,可以正常工作.

Tested and works.

这篇关于Woocommerce基于购物车中物品总数的累进额外费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 11:58