1

This is WordPress website integrated with woocommerce. In this store, they are two main categories Category1 and Category2.

I want to make two different set of discounts that should be applied with respect to 2 different categories.

For instance:

Scenario For Category 1:

  1. Buy $500-$999 and get 5% Discount
  2. Buy $1000-$2000 and get 15% Discount
  3. Buy $2001-$5000 and get 25% Discount

Scenario For Category 2:

  1. Buy $500-$999 and get 20% Discount
  2. Buy $1000-$2000 and get 40% Discount
  3. Buy $2001-$5000 and get 60% Discount

Consider if I buy the product1 & product2 from Category1 worth $1000 and product5 & product6 from Category2 worth $4000.

So the summary cart should show like below:

    Cart subtotal:                       $5000
    Category 1 Discount (15%) of $1000: -$150
    Category 2 Discount (60%) of $4000  -$2400
    Amount to Pay:                       $2450

The discount should be applied based on the subtotal of each category. I have tried many plugins and customized the code and I could not achieve the functionality in woocoomerce.

Could you please suggest any plugins or custom code to acheive this functionality?

UPDATE:

I have tried the below code based on the thread Auto apply a percentage or fixed cart discount based on the total in WooCommerce.

Tried to apply progressive discount based on 2 conditions.

  1. Based on the cart value
  2. With respect to specific category.

But the code tried is not working. What I need to change in this code? I need to set the progressive discount for 2 different categories.

add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_wise_discount', 10, 1 );

    function cart_items_quantity_wise_discount($cart_object) {

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

        // Set HERE your category (can be an ID, a slug or the name)
        $category = '224'; // our category id

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

        // Iterating through each cart item
        foreach($cart_object->get_cart() as $cart_item):
        //print_r($cart_item);exit;
            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' );

        if ( $category_total >=2000 && $category_total <=2999 ) {
            $discount -= $category_total * 0.3; // Discount of 10% 
            $discount_text_output = $discount_text . '10%';
        } 
        elseif ( $category_total >=3000 && $category_total <=4999 ) {
            $discount -= $category_total * 0.15; // Discount of 15%
            $discount_text_output = $discount_text . '15%';
        }
    // 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)
    }

SECOND UPDATE (version 2) - I have tried the below code. But category wise discount not affected on my checkout page. Could you please look into this and what i am doing wrong in this code?

add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );
function cart_items_quantity_multiple_discounts( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE set your 2 product category term names
    $category1 = 'Appliances';
    $category2 = 'Electronics';

    $total1 = $total2 = $percentage1 = $percentage2 = 0;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item ) {
        if( has_term( $category1, 'product_cat', $cart_item['product_id']) ) {
            $total1 += $cart_item["line_total"]; // Excluding taxes
        } elseif( has_term( $category2, 'product_cat', $cart_item['product_id']) ) {
            $total2 += $cart_item["line_total"]; // Excluding taxes
        }
    }

    // All Amounts need to be set without taxes

    // First category "Appliances" progressive percentage discount
    if ( $total1 >= 1000 && $total1 < 1500 ) { // <== set excluding taxes 
        $percentage1 = 5;
    }elseif ( $total1 >= 1500 && $total1 < 2500 ) { // <== set excluding taxes 
        $percentage1 = 10;
    }elseif ( $total1 >= 2500 ) { // <== set excluding taxes amounts
        $percentage1 = 15;
    }

    // Second category "Electronics" progressive percentage discount
    if ( $total2 >= 1000 && $total2 < 1500 ) { // <== set excluding taxes amounts
        $percentage2 = 10;
    }elseif ( $total2 >= 1500 && $total2 < 2500 ) { // <== set excluding taxes amounts
        $percentage2 = 15;
    }elseif ( $total2 >= 2500 ) { // <== set excluding taxes amounts
        $percentage2 = 20;
    }

    // Set the first discount for "Appliances"
    if( $percentage1 > 0 ){
        $discount1   = $total1 * $percentage1 / 100;
        $label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
            $category1, $percentage1 . '%', strip_tags(wc_price($total1)));
        $cart->add_fee( $label_text1, -$discount1 );
    }

    // Set the Second discount for "Electronics"
    if( $percentage2 > 0 ){
        $discount2   = $total2 * $percentage2 / 100;
        $label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
            $category2, $percentage2 . '%', strip_tags(wc_price($total2)));
        $cart->add_fee( $label_text2, -$discount2 );
    }

    // Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Vinayagam
  • 944
  • 1
  • 9
  • 29

1 Answers1

1

@Vinayagam - Please use the below code in your child theme functions.php. I have tested and working fine.

    add_action( 'woocommerce_cart_calculate_fees', 'cart_items_quantity_multiple_discounts', 10, 1 );

    function cart_items_quantity_multiple_discounts( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;

        // HERE set your 2 product category term names
        $category1 = 250;//Appliances
        $category2 = 224;//Electronics

        $pdt_id_1 = $pdt_id_2 = $total1 = $total2 = $percentage1 = $percentage2 = 0;
        //$pdt_id_1 - Category One;
        //$pdt_id_2 - Category Two;
enter code here
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ) {
            $_product = $cart_item['data'];
            $terms = get_the_terms( $_product->id, 'product_cat' );
            foreach ($terms as $term) {
                $_categoryid = $term->parent;
                if ( $_categoryid === $category1 and $pdt_id_1!=$_product->id) {
                    $total1 += $cart_item["line_total"];
                    $pdt_id _1= $_product->id;
                }
                else if ( $_categoryid === $category2 and $pdt_id_2!=$_product->id) {
                    $total2 += $cart_item["line_total"];
                    $pdt_id_2 = $_product->id;
                }
            }
        }

        // All Amounts need to be set without taxes

        // First category "Appliances" progressive percentage discount
        if ( $total1 >= 2000 && $total1 <= 2999 ) {
            $percentage1 = 3;
        }elseif ( $total1 >= 3000 && $total1 <= 4999 ) {
            $percentage1 = 5;
        }elseif ( $total1 >= 5000 && $total1 <= 9999) {
            $percentage1 = 8;
        }elseif ( $total1 >= 10000 && $total1 <= 14999) {
            $percentage1 = 10;
        }elseif ( $total1 >= 15000 && $total1 <= 24999) {
            $percentage1 = 13;
        }elseif ( $total1 >= 25000 && $total1 <= 29999) {
            $percentage1 = 15;
        }elseif ( $total1 >= 30000 && $total1 <= 49999) {
            $percentage1 = 18;
        }elseif ( $total1 >= 50000 && $total1 <= 59999) {
            $percentage1 = 19;
        }elseif ( $total1 >= 60000 && $total1 <= 499999) {
            $percentage1 = 20;
        }

        // Second category "Electronics" progressive percentage discount
        if ( $total2 >= 2000 && $total2 <= 2999 ) {
            $percentage2 = 5;
        }elseif ( $total2 >= 3000 && $total2 <= 4999 ) {
            $percentage2 = 10;
        }elseif ( $total2 >= 5000 && $total2 <= 9999 ) {
            $percentage2 = 15;
        }elseif ( $total2 >= 10000 && $total2 <= 14999 ) {
            $percentage2 = 20;
        }elseif ( $total2 >= 15000 && $total2 <= 24999 ) {
            $percentage2 = 25;
        }elseif ( $total2 >= 25000 && $total2 <= 29999 ) {
            $percentage2 = 30;
        }elseif ( $total2 >= 30000 && $total2 <= 49999 ) {
            $percentage2 = 40;
        }elseif ( $total2 >= 50000 && $total2 <= 59999 ) {
            $percentage2 = 45;
        }elseif ( $total2 >= 60000 && $total2 <= 499999 ) {
            $percentage2 = 50;
        }

        // Set the first discount for "Appliances"
        if( $percentage1 > 0 ){
            $discount1   = $total1 * $percentage1 / 100;
            $label_text1 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
                'Appliances', $percentage1 . '%', strip_tags(wc_price($total1)));
            $cart->add_fee( $label_text1, -$discount1, true, '');
        }

        // Set the Second discount for "Electronics"
        if( $percentage2 > 0 ){
            $discount2   = $total2 * $percentage2 / 100;
            $label_text2 = sprintf( __( '%s Discount (%s) of %s', 'woocommerce' ),
                'Electronics', $percentage2 . '%', strip_tags(wc_price($total2)));
            $cart->add_fee( $label_text2, -$discount2 );
        }
        // Note: Last argument "taxable" in add_fee() method is always true for negative fees (discounts)
    }
vetrivel
  • 178
  • 7