【问题标题】:WooCommerce progressive quantity discount for specific product categoriesWooCommerce 针对特定产品类别的累进数量折扣
【发布时间】:2021-03-14 20:11:58
【问题描述】:

我正在研究一种方法,如果您的购物车中有超过 1 个产品,则可以提供累进折扣。我找到了this thread 并实际使用了这段代码:

//Discount by Qty Product
add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );

function quantity_based_pricing( $cart ) {
   global $product;
   if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

   // Define discount rules and thresholds and product ID
   $threshold1 = 2; // Change price if items > 1
   $discount1 = 10; // Reduce unit flat price by 10
   $threshold2 = 3; // Change price if items > 2
   $discount2 = 20; // Reduce unit flat price by 20

   foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( $product_in_cart === $product_id && $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
           $price = round( $cart_item['data']->get_price() - $discount1, 2 );
           $cart_item['data']->set_price( $price );
        } elseif ( $product_in_cart === $product_id && $cart_item['quantity'] >= $threshold2 ) {
           $price = round( $cart_item['data']->get_price() - $discount2, 2 );
           $cart_item['data']->set_price( $price );
        }    
      }
}

为了使其更具动态性,我想为特定类别制定此规则,因此每次我在该类别中添加产品时,都会应用该规则。 我尝试使用

  • is_product_category ('example')
  • if (is_product_category () &amp;&amp; has_term ('example', 'product_cat') return;

我没有成功,谁能告诉我如何创造这个条件。

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

    您的代码中缺少针对特定产品类别的内容。请尝试以下操作:

    add_action( 'woocommerce_before_calculate_totals', 'quantity_based_pricing', 9999 );
    function quantity_based_pricing( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
           return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) 
            return;
    
        // Define categories and discount rules (quantity threshold and discount amount)
        $categories = array('example');
        $threshold1 = 2;  // Change price if items > 1
        $discount1  = 10; // Reduce unit flat price by 10
        $threshold2 = 3;  // Change price if items > 2
        $discount2  = 20; // Reduce unit flat price by 20
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Target specific product category(ies)
            if ( has_term ($categories, 'product_cat', $cart_item['product_id']) ) {
                if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
                    $price = round( $cart_item['data']->get_price() - $discount1, 2 );
                    $cart_item['data']->set_price( $price );
                } elseif ( $cart_item['quantity'] >= $threshold2 ) {
                    $price = round( $cart_item['data']->get_price() - $discount2, 2 );
                    $cart_item['data']->set_price( $price );
                }
            }    
        }
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 再次感谢您的快速回复。所以我只需要创建$categories,要反转我必须输入$exclude
    • @IamLearningPHP 相反使用!like if ( ! has_term ($categories, 'product_cat', $cart_item['product_id']) ) {... 现在这个答案回答了你的问题和工作,你可以请accept 回答,如果你喜欢/想要你可以请也upvote答案,谢谢。
    • 我做了更多测试,发现该规则不适用于可变产品,是否有任何解决方案可以将其应用于该类别中的所有项目? Example: Category Pet > 2x Dog Collar + 1x Cat Collar 将累进折扣规则应用于“宠物”类别中的所有产品
    • @IamLearningPHP 该规则也适用于可变产品,因为$cart_item['product_id']是产品变体购物车项目的父可变产品的 ID(请记住,产品变体不处理任何分类作为产品类别,但他们的父变量产品是......
    猜你喜欢
    • 2021-06-10
    • 2021-01-29
    • 2020-04-26
    • 2021-05-18
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多