WooCommerce progressive quantity discount for specific product categories

余生长醉 提交于 2021-02-10 15:54:07

问题


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 ('example')
  • if (is_product_category () && has_term ('example', 'product_cat') return;

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


回答1:


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

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



来源:https://stackoverflow.com/questions/65115355/woocommerce-progressive-quantity-discount-for-specific-product-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!