基于数量计算的产品类别的购物车折扣 [英] Cart discount for a product category based on quantity calculations

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

问题描述

我想向woocommerce添加一个功能,当将一个类别中的12-23个商品添加到购物车时,该功能将计算10%的折扣。

I wan to add a function to woocommerce that will calculate a 10% discount when 12-23 items from one category is added to the cart.

然后,如果24 -添加了该类别的47个商品,将有15%的折扣。

Then if 24 - 47 items of the category are added it would be a 15% discount.

最后,如果添加了该类别的48个以上的商品,将有20%的折扣。

Last if 48+ items from this category are added it would be a 20% discount.

实际代码例子太棒了,因为我是woocommerce的新手

actual code example would be awesome as I am new to woocommerce

推荐答案


已更新更正了代码错误,并在输出的折扣文本中添加了增强功能

此处是挂钩在 woocommerce_cart_calculate_fees 挂钩,该挂钩将根据购物车项目数量计算为该特定类别(或子类别)提供折扣。

Here is the function hooked in woocommerce_cart_calculate_fees hook that is going to make the discount for that particular category (or subcategory too) based on cart item quantity calculations.

这是代码:

add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_wine_discount', 10, 1 );
function cart_items_quantity_wine_discount($cart_object) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Set HERE your category (can be an ID, a slug or the name)
    $category = 34; // or a slug: $category = 'wine';

    $category_count = 0;
    $category_total = 0;
    $discount = 0;

    // Iterating through each cart item
    foreach($cart_object->get_cart() as $cart_item):

        if( has_term( $category, 'product_cat', $cart_item['product_id']) ):
            $category_count += $cart_item['quantity'];
            $category_total += $cart_item["line_total"]; // calculated total items amount (quantity x price)
        endif;

    endforeach;

    $discount_text = __( 'Quantity discount of ', 'woocommerce' );

    // ## CALCULATIONS ##
    if ( $category_count >= 12 && $category_count < 24 ) {
        $discount -= $category_total * 0.1; // Discount of 10% 
        $discount_text_output = $discount_text . '10%';
    } elseif ( $category_count >= 24 && $category_count < 48 ) {
        $discount -= $category_total * 0.15; // Discount of 15%
        $discount_text_output = $discount_text . '15%';
    } elseif ( $category_count >= 48 ) {
        $discount -= $category_total * 0.2; // Discount of 20%
        $discount_text_output = $discount_text . '20%';
    }

    // Adding the discount
    if ( $discount != 0 && $category_count >= 12 )
        $cart_object->add_fee( $discount_text_output, $discount, false );

    // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}




注意: > add_fee() 方法与应征税款还是不对折扣有关……

Note: Last argument in add_fee() method is related to applying the tax or not to the discount…

代码已经过测试并且功能齐全。

Code is tested and fully functional.

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

其他类似:基于产品总数的某些类别的折扣

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

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