WooCommerce购物车数量基础折扣 [英] WooCommerce Cart Quantity Base Discount

查看:146
本文介绍了WooCommerce购物车数量基础折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,如何根据购物车中的商品总数设置购物车折扣?

In WooCommerce, how do I set a cart discount based on the total number of items in the cart?

例如:

  • 1至4件-不打折
  • 5至10件-5%
  • 11至15件-10%
  • 16到20件-15%
  • 21至25件-20%
  • 26至30个项目-25%

我已经搜索了互联网,但未找到任何可用的解决方案或插件.

I've search internet but not found any solution or plugins available.

谢谢.

推荐答案

您可以使用购物车负费用获得折扣.然后,您将添加条件&通过以下方式挂钩到 woocommerce_cart_calculate_fees 动作挂钩中的自定义函数的计算:

You can use a negative cart fee to get a discount. Then you will add your conditions & calculations to a acustom function hooked in woocommerce_cart_calculate_fees action hook, this way:

## Tested and works on WooCommerce 2.6.x and 3.0+
add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );
function wc_cart_quantity_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## -------------- DEFINIG VARIABLES ------------- ##
    $discount = 0;
    $cart_item_count = $cart_object->get_cart_contents_count();
    $cart_total_excl_tax = $cart_object->subtotal_ex_tax;

    ## ----------- CONDITIONAL PERCENTAGE ----------- ##
    if( $cart_item_count <= 4 )
        $percent = 0;
    elseif( $cart_item_count >= 5 && $cart_item_count <= 10 )
        $percent = 5;
    elseif( $cart_item_count > 10 && $cart_item_count <= 15 )
        $percent = 10;
    elseif( $cart_item_count > 15 && $cart_item_count <= 20 )
        $percent = 15;
    elseif( $cart_item_count > 20 && $cart_item_count <= 25 )
        $percent = 20;
    elseif( $cart_item_count > 25 )
        $percent = 25;


    ## ------------------ CALCULATION ---------------- ##
    $discount -= ($cart_total_excl_tax / 100) * $percent;

    ## ----  APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
    if( $percent > 0 )
        $cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

经过测试并可以在WooCommerce 2.6.x和3.0+上运行

Tested and works on WooCommerce 2.6.x and 3.0+

这篇关于WooCommerce购物车数量基础折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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