针对特定产品类别的WooCommerce渐进式数量折扣 [英] WooCommerce progressive quantity discount for specific product categories

查看:58
本文介绍了针对特定产品类别的WooCommerce渐进式数量折扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种方法,如果您的购物车中有超过1种产品,则可以给予逐步折扣.我找到了此线程,并实际上使用了此代码:

I was researching a method to give a progressive discount if you have more than 1 product in the cart. I found this thread and actually using this code:

//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 );
        }    
      }
}

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

To make it more dynamic I thought of making this rule for a specific category, so every time I add a product in that category the rule will be applied. I tried to use

  • is_product_category(示例")
  • 如果(is_product_category()&& has_term('example','product_cat')返回;

我没有成功,有人可以告诉我如何创建这种条件.

and I was not successful could someone tell me how to create this condition.

推荐答案

您的代码中缺少一些针对特定产品类别的东西.请尝试以下操作:

There is something missing in your code to target specific product category. Try the following:

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文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

这篇关于针对特定产品类别的WooCommerce渐进式数量折扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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