Welcome to the support center for

Fancy Product Designer & Multistep Product Configurator

Snippet to generate discount by volume of purchase in the car

Hello everyone

I found this snippet on the web to generate discount for volume of purchase.

This works without using the coupon discount but does not replace the coupon if it is active, if you want more information I leave the link.

https://www.themelocation.com/how-to-apply-bulk-discount-in-woocommerce/#codesyntax_1



I hope Mr Radikal can integrate something similar to the Bulk-Add Form to generate the price of the product in bulk purchase with the discount already applied when the customer puts the quantities and do not scare the customer when he puts 50 T-shirts for $ 20 each.


If you are in agreement vote for this solution, thanks


*****************************************


///// Paste this snippet it into your /theme-child/functions.php


add_action('woocommerce_cart_calculate_fees', 'tl_apply_bulk_discount');

function tl_apply_bulk_discount() {

 global $woocommerce;

 $excluded_amount = $discount_percent = 0;

  $working_total   = $woocommerce->cart->cart_contents_total;

  $excluded_categories = array(

 217, # Training

 223, # Starter Kits

 );

 

 # Only apply manual discount if no coupons are applied

 if (!$woocommerce->cart->applied_coupons) {

 

 # Find any items in cart that belong to the restricted categories

 foreach ($woocommerce->cart->cart_contents as $item) {

 $product_categories = get_the_terms($item['product_id'], 'product_cat');

      if (empty($product_categories) || is_wp_error($product_categories) || !$product_categories) {

 if (is_wp_error($product_categories)) {

 wp_die($product_categories->get_error_message());

 }

 else {

 $product_categories = new WP_Error('no_product_categories', "The product \"".$item->post_title."\" doesn't have any categories attached, thus no discounts can be calculated.", "Fatal Error");

 wp_die($product_categories);

 }

 }

 foreach ($excluded_categories as $excluded_category) {

 foreach ($product_categories as $category) {

 if ($excluded_category == $category->term_id) {

 $excluded_amount += $item['line_subtotal']; # Increase our discounted amount

            $working_total -= $item['line_subtotal'];   # Decrease our discounted amount

 }

 }

 }

 }

 

 # Logic to determine WHICH discount to apply based on subtotal

if ($working_total >= 200 && $working_total < 344) {

 $discount_percent = 5;

 }

 elseif ($working_total >= 344 && $working_total < 425) {

 $discount_percent = 8;

 }

 elseif ($working_total >= 425 && $working_total < 510) {

 $discount_percent = 11;

 }

 elseif ($working_total >= 510 && $working_total < 595) {

 $discount_percent = 14;

 }

 elseif ($working_total >= 595 && $working_total < 680) {

 $discount_percent = 17;

 }

 elseif ($working_total >= 680 && $working_total < 765) {

 $discount_percent = 20;

 }

 elseif ($working_total >= 765 && $working_total < 850) {

 $discount_percent = 23;

 }

 elseif ($working_total >= 850 && $working_total < 935) {

 $discount_percent = 26;

 }

 elseif ($working_total >= 935 && $working_total < 1020) {

 $discount_percent = 29;

 }

 elseif ($working_total >= 1020 && $working_total < 1105) {

 $discount_percent = 32;

 }

 elseif ($working_total >= 1105 && $working_total < 1190) {

 $discount_percent = 35;

 }

 elseif ($working_total >= 1190 && $working_total < 1275) {

 $discount_percent = 38;

 }

 elseif ($working_total >= 1275 && $working_total < 1360) {

 $discount_percent = 41;

 }

 elseif ($working_total >= 1360 && $working_total < 1445) {

 $discount_percent = 44;

 }

 elseif ($working_total >= 1445 && $working_total < 1530) {

 $discount_percent = 47;

 }

 elseif ($working_total >= 1530 && $working_total < 1615) {

 $discount_percent = 50;

 }

 elseif ($working_total >= 1615 && $working_total < 1700) {

 $discount_percent = 53;

 }

 elseif ($working_total >= 1700 && $working_total < 2534) {

 $discount_percent = 56;

 }

 elseif ($working_total >= 2534 && $working_total < 3384) {

 $discount_percent = 57;

 }

 elseif ($working_total >= 3384 && $working_total < 4234) {

 $discount_percent = 58;

 }

 elseif ($working_total >= 4234 && $working_total < 5934) {

 $discount_percent = 60;

 }

 elseif ($working_total >= 5934 && $working_total < 7634) {

 $discount_percent = 61;

 }

 elseif ($working_total >= 7634 && $working_total < 9334) {

 $discount_percent = 62;

 }

 elseif ($working_total >= 9334 && $working_total < 11034) {

 $discount_percent = 63;

 }

 elseif ($working_total >= 11034 && $working_total < 12734) {

 $discount_percent = 64;

 }

 elseif ($working_total >= 12734 && $working_total < 14434) {

 $discount_percent = 65;

 }

 elseif ($working_total >= 14434 && $working_total < 16134) {

 $discount_percent = 67;

 }

 elseif ($working_total >= 16134 && $working_total < 17834) {

 $discount_percent = 68;

 }

 elseif ($working_total >= 17834 && $working_total < 19534) {

 $discount_percent = 69;

 }

elseif ($working_total >= 19534 && $working_total < 185000) {

 $discount_percent = 70;

 }

 else {

 $discount_percent = 0;

 }

 

 # Make sure cart total is eligible for discount

 if ($discount_percent > 0) {

      $discount_amount  = ( ( ($discount_percent/100) * $working_total ) * -1 );

 $woocommerce->cart->add_fee('Bulk Discount', $discount_amount);

 }

 }

}



Login or Signup to post a comment