简体   繁体   English

折扣基于 WooCommerce 购物车中某个类别的商品数量计数

[英]Discount based on item quantity count for a category in WooCommerce cart

I have a category of products that are all priced at $15.我有一类产品的价格都是 15 美元。 When the user buys between 10 & 20 products from this category they should receive a discounted price of $10.当用户从该类别购买 10 到 20 件产品时,他们应该会收到 10 美元的折扣价。 When the user buys 20+ the price changes again to $5.当用户购买 20+ 时,价格再次变为 5 美元。 The user cannot have a custom role assigned to them (like wholesaler).不能为用户分配自定义角色(如批发商)。 I created code loosely based on LoicTheAztec code from another question and added my own modifications and code.我根据另一个问题的 LoicTheAztec 代码松散地创建了代码,并添加了我自己的修改和代码。 It looks like it should work.看起来它应该工作。 I receive no errors but it does not work.我没有收到任何错误,但它不起作用。

add_action('woocommerce_before_cart', 'check_product_category_in_cart');

function check_product_category_in_cart() {
    // HERE set your product categories in the array (can be IDs, slugs or names)
    $categories = array('surfacing-adhesives');
    $found      = false; // Initializing

    $count = 0;

    // Loop through cart items      
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If product categories is found
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $count++;
        }
    }

    if (!current_user_can('wholesaler')) {
        // Discounts
        if ($count > 10 && $count < 20) {
            // Drop the per item price
            $price = 10;
        } else if ($count > 20) {
            // Drop the per item price
            $price = 5;
        } else {
            // Did not qualify for volume discount
        }
    }
}

You are not using the correct hook and there are some missing things.您没有使用正确的钩子,并且缺少一些东西。 Try the following:尝试以下操作:

add_action( 'woocommerce_before_calculate_totals', 'discounted_cart_item_price', 20, 1 );
function discounted_cart_item_price( $cart ){
    // Not for wholesaler user role
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || current_user_can('wholesaler') )
        return;

    // Required since Woocommerce version 3.2 for cart items properties changes
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE set your product categories in the array (can be IDs, slugs or names)
    $categories = array('surfacing-adhesives');
    $categories = array('t-shirts');

    // Initializing
    $found = false;
    $count = 0;

    // 1st Loop: get category items count  
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // If product categories is found
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $count += $cart_item['quantity'];
        }
    }

    // Applying discount
    if ( $count >= 10 ) {
        // Discount calculation (Drop the per item qty price)
        $price = $count >= 20 ? 5 : 10;

        // 2nd Loop: Set discounted price  
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // If product categories is found
            if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
                $cart_item['data']->set_price( $price );
            }
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 购物商品折扣基于Woocommerce 3中的数量 - Cart item discount based on quantity in Woocommerce 3 WooCommerce 购物车折扣:根据商品数量免费赠送一件商品 - WooCommerce cart discount: one item free based on item quantity Woocommerce中基于产品类别的第二项的数量折扣 - Quantity Discount for 2nd Item Based on Product Category in Woocommerce 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce Woocommerce 3中的购物车项目数量累进百分比折扣 - Cart item quantity progressive percentage discount in Woocommerce 3 "基于数量计算的产品类别的购物车折扣" - Cart discount for a product category based on quantity calculations 基于 Woocommerce 中购物车项目数的有条件累进百分比折扣 - Conditional progressive percentage discount based on cart item count in Woocommerce 根据Woocommerce中的产品类别将最大商品数量添加到购物车 - Max item quantity added to cart based on product category in Woocommerce 根据 WooCommerce 产品类别禁用特定的购物车项目数量字段 - Disable specific cart item quantity fields based on WooCommerce product category Woocommerce 基于数量的每件商品百分比折扣 - Woocommerce percentage discount per item based on quantity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM