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:
- Buy $500-$999 and get 5% Discount
- Buy $1000-$2000 and get 15% Discount
- Buy $2001-$5000 and get 25% Discount
Scenario For Category 2:
- Buy $500-$999 and get 20% Discount
- Buy $1000-$2000 and get 40% Discount
- 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.
- Based on the cart value
- 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)
}