Filter uncanny-learndash-groups

ulgm_apply_bulk_discount_on_fixed_price

Filters if a bulk discount should be applied to products with fixed prices before processing.

add_filter( 'ulgm_apply_bulk_discount_on_fixed_price', $callback, 10, 1 );

Description

Filters whether a bulk discount is applied to a product with a fixed price. Developers can use this hook to conditionally prevent bulk discounts from being applied to specific products, overriding default plugin behavior. Returns `false` to disable the discount.


Usage

add_filter( 'ulgm_apply_bulk_discount_on_fixed_price', 'your_function_name', 10, 1 );

Parameters

$_product (mixed)
This parameter is a boolean that, when set to `true`, forces the bulk discount to be applied to the product.

Return Value

The filtered value.


Examples

/**
 * Example filter to conditionally disable bulk discounts on products with a fixed license price.
 *
 * In this example, we assume that if a product has a fixed license price set,
 * we might want to prevent standard bulk discounts from applying to it, perhaps
 * to ensure the fixed license price remains the primary pricing mechanism.
 *
 * @param bool $_product_id The product ID being processed.
 * @param WC_Product $_product The WooCommerce product object.
 * @return bool True to allow bulk discount, false to prevent it.
 */
add_filter( 'ulgm_apply_bulk_discount_on_fixed_price', function( $allow_discount, $_product ) {

	// Check if the product is an instance of WC_Product
	if ( ! $_product instanceof WC_Product ) {
		return $allow_discount; // Return original value if not a valid product
	}

	// Get the product ID
	$product_id = $_product->get_id();

	// If this is a product where a fixed license price is set,
	// and we want to prevent bulk discounts on such items, return false.
	// This assumes a function like 'is_fixed_license_price_product' exists
	// to determine if a product has a fixed license price.
	if ( function_exists( 'is_fixed_license_price_product' ) && is_fixed_license_price_product( $product_id ) ) {
		// We're explicitly preventing the bulk discount for fixed license price products.
		return false;
	}

	// Otherwise, allow the default behavior for applying bulk discounts.
	return $allow_discount;

}, 10, 2 ); // Priority 10, accepts 2 arguments

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/woocommerce/woocommerce-bulk-discount.php:457
src/classes/woocommerce/woocommerce-bulk-discount.php:518

public function calculate_cart_discounts_v2( WC_Cart $cart ) {
		if ( ! $this->is_bulk_discount_enabled() ) {
			return;
		}
		$this->gather_discount_coeffs();
		if ( ! isset( $cart->cart_contents ) || 0 === count( $cart->cart_contents ) ) {
			return;
		}
		foreach ( $cart->cart_contents as $values ) {
			$_product = $values['data'];
			if (
				WoocommerceMinMaxQuantity::is_fixed_price_set_for_the_license( $_product->get_id() ) &&
				false === apply_filters( 'ulgm_apply_bulk_discount_on_fixed_price', false, $_product->get_id() )
			) {
				continue;
			}
			$_price       = $_product->get_price();
			$subtract_fee = 0;
			if ( ! array_key_exists( absint( $_product->get_id() ), $this->discount_coeffs ) ) {
				continue;
			}
			$coeff = $this->discount_coeffs[ absint( $_product->get_id() ) ]['coeff'];
			WC()->cart->fees_api()->remove_all_fees();

			$apply_coeff = 0;

			if ( SharedFunctions::is_group_licensed_product( $_product ) ) {
				$_quantity   = $values['quantity'];
				$_price      = floatval( $_price ) * absint( $_quantity );
				$apply_coeff = 1;
			}

			if ( 1 == $coeff ) {
				return;
			}
			if ( 1 === $apply_coeff ) {
				$fee_name           = sprintf( __( '%s%% bulk discount', 'uncanny-learndash-groups' ), ( 1 - $coeff ) * 100 );
				$discount           = ( ( $_price + $subtract_fee ) * ( 1 - $coeff ) ) * - 1;
				$fee_to_add         = new stdClass();
				$fee_to_add->amount = $discount;
				$fee_to_add->total  = $discount;
				$fee_to_add->name   = $fee_name;
				WC()->cart->fees_api()->add_fee( $fee_to_add );
				// Remove recurring totals and add only relevant bulk discount
				if ( ! empty( $cart->recurring_cart_key ) ) {
					$cart->fees_api()->remove_all_fees();
					$cart->fees_api()->add_fee( $fee_to_add );
				}
			}
		}
		$this->bulk_discount_calculated = true;
	}

Scroll to Top