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

查看:39
本文介绍了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天全站免登陆