Bulk discount

<?php

// sanders

/**
 * Adding custom bulk-discount option for cart content
 */
function add_custom_bulk_fee( ) {
  global $woocommerce;
  $subtotal = $woocommerce->cart->subtotal;
//   $subtotal = $woocommerce->cart->cart_contents_total; // + $woocommerce->cart->shipping_total;

    // Count the total amount of packages
    $total_packages = 0;
    foreach($woocommerce->cart->get_cart() as $cart_item) {
        if($cart_item['data']->is_type('yith_bundle')) {
            $total_packages += $cart_item['quantity'];
        }
    }

    // Loop through each package and apply discount
    foreach($woocommerce->cart->get_cart() as $cart_item) {
      if($cart_item['data']->is_type('yith_bundle')) {

          $items = $total_packages;
          if($items >= 25 && $items < 50) {
            $discountPercentage = 3;
            $discount = ($subtotal / 100 * $discountPercentage);
            $woocommerce->cart->add_fee( sprintf(esc_html__( 'U heeft %1$s pakketten. Er is %2$s&#37; korting verrekend', 'sandersgifts' ), $items, $discountPercentage ), -$discount);
          }

          if($items >= 50 && $items < 100) {
            $discountPercentage = 6;
            $discount = ($subtotal / 100 * $discountPercentage);
            $woocommerce->cart->add_fee( sprintf(esc_html__( 'U heeft %1$s pakketten. Er is %2$s&#37; korting verrekend', 'sandersgifts' ), $items, $discountPercentage ), -$discount);
          }

          if($items >= 100 && $items < 250) {
            $discountPercentage = 9;
            $discount = ($subtotal / 100 * $discountPercentage);
            $woocommerce->cart->add_fee( sprintf(esc_html__( 'U heeft %1$s pakketten. Er is %2$s&#37; korting verrekend', 'sandersgifts' ), $items, $discountPercentage ), -$discount);
          }

          if($items >= 250 && $items < 500) {
            $discountPercentage = 12;
            $discount = ($subtotal / 100 * $discountPercentage);
            $woocommerce->cart->add_fee( sprintf(esc_html__( 'U heeft %1$s pakketten. Er is %2$s&#37; korting verrekend', 'sandersgifts' ), $items, $discountPercentage ), -$discount);
          }

          if($items >= 500) {
            $discountPercentage = 15;
            $discount = ($subtotal / 100 * $discountPercentage);
            $woocommerce->cart->add_fee( sprintf(esc_html__( 'U heeft %1$s pakketten. Er is %2$s&#37; korting verrekend', 'sandersgifts' ), $items, $discountPercentage ), -$discount);
          }
      }
    }

  return $cart;
}
add_action('woocommerce_cart_calculate_fees' , 'add_custom_bulk_fee');


// Disable tax calculations for added fees (see: https://github.com/woocommerce/woocommerce/issues/21148)
  add_action('woocommerce_cart_totals_get_fees_from_cart_taxes', function() {
    return [];
}, 10, 3);
Last Updated:
Contributors: Niek Vlam, Suite Seven