Filter uncanny-learndash-groups

ulgm_recurring_bulk_discount

Filters the recurring bulk discount details before it is applied to the cart.

add_filter( 'ulgm_recurring_bulk_discount', $callback, 10, 3 );

Description

Fires when a recurring bulk discount is potentially applied to a subscription product. Developers can use this filter to modify the discount's application status. The hook returns `true` to apply the discount or `false` to prevent it. It passes the current discount status, fee details, and cart object.


Usage

add_filter( 'ulgm_recurring_bulk_discount', 'your_function_name', 10, 3 );

Parameters

$current (mixed)
This parameter likely represents whether the current fee is considered a recurring fee or not, potentially being a boolean value.
$fee (mixed)
This parameter holds the current value of the recurring discount, which might be modified by the filter.
$cart (mixed)
This parameter represents the fee object being processed, which may contain details like its name.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the ulgm_recurring_bulk_discount filter.
 * This example modifies the discount amount for recurring bulk discounts.
 *
 * @param bool  $current The current value of the recurring bulk discount (true if it should be applied).
 * @param mixed $fee     The fee object.
 * @param mixed $cart    The WC_Cart object.
 *
 * @return bool The modified value of the recurring bulk discount.
 */
add_filter( 'ulgm_recurring_bulk_discount', 'my_custom_recurring_bulk_discount_logic', 10, 4 );

function my_custom_recurring_bulk_discount_logic( $apply_discount, $current, $fee, $cart ) {
    // Check if we're in the context where this filter is expected to be applied
    // (e.g., within a subscription and the fee name indicates a bulk discount).
    // The original function already handles some of these checks, but this is
    // a demonstration of how to add more logic.

    // Example: Let's say we want to reduce the recurring bulk discount by an additional 5%
    // if the total subscription value in the cart exceeds a certain threshold.
    $threshold = 500; // Example threshold for total subscription value
    $additional_discount_percentage = 0.05; // 5%

    $subscription_total = 0;
    // This is a simplified way to get subscription totals; in a real scenario,
    // you might need a more robust method depending on how subscriptions are structured.
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( isset( $cart_item['subscription_data']['price'] ) ) {
            $subscription_total += $cart_item['subscription_data']['price'];
        }
    }

    if ( $subscription_total > $threshold ) {
        // If the subscription total is above the threshold, we might want to
        // *not* apply the recurring bulk discount at all, or perhaps adjust it.
        // For this example, let's say we want to prevent the discount if the
        // subscription is already very high value.
        return false; // Do not apply the recurring bulk discount in this case.
    }

    // If the above condition isn't met, we let the original logic decide,
    // or we could modify the 'true' value here if needed.
    // For instance, if we wanted to ensure it's always applied if it reached here
    // and the original $current was false, we could do:
    // return true;

    // In this specific example, we're mainly demonstrating how to *prevent* the discount.
    // If we wanted to *reduce* it by a percentage, we'd need access to the actual
    // discount amount which isn't directly passed here.
    // The $current parameter is likely a boolean indicating if the discount *should* be applied.

    // Let's assume the original function's logic determined it *should* be applied ($current = true)
    // and we want to add a condition to *prevent* it.
    // The original function returns `apply_filters( 'ulgm_recurring_bulk_discount', true, $current, $fee, $cart );`
    // So, if $current is true, the filter receives `true`.
    // If we want to *further* condition based on the $fee or $cart itself:

    // Another example: only apply if the fee name contains 'premium'
    if ( isset( $fee->name ) && strpos( $fee->name, 'premium' ) !== false ) {
        // If the fee name is 'premium bulk discount', then we allow the $current value to pass through.
        // If $current was true, it remains true. If $current was false, it remains false.
        return $current;
    } else {
        // If it's not a 'premium' bulk discount, then we explicitly prevent it,
        // regardless of what $current was.
        return false;
    }

    // For the original intention of modifying the discount amount itself, you would typically
    // need the discount value passed to the filter. Since it's not, we're working with
    // the boolean $current parameter which likely controls application.

    // Let's simplify and show a basic modification of the boolean:
    // if ( $current === true ) {
    //     // If the filter is being asked to apply the discount,
    //     // and the cart contains more than 5 items, then don't apply it.
    //     if ( $cart->get_cart_contents_count() > 5 ) {
    //         return false; // Prevent the discount
    //     }
    // }
    // return $current; // Otherwise, let the original value decide.

    // For this specific hook, the intention is likely to control whether the discount is *applied* ($true/$false).
    // We will stick to modifying the boolean $current.

    // Final example: Only allow the recurring bulk discount if it's a subscription and the cart total is over $100.
    // This is somewhat redundant with the parent function's check for subscription,
    // but demonstrates filtering the boolean.
    if ( $current === true && $cart->get_subtotal() > 100 ) {
        // If the discount was intended to be applied and the cart subtotal is sufficient
        return true; // Explicitly allow it (effectively returning $current if $current was true)
    } elseif ( $current === true && $cart->get_subtotal() <= 100 ) {
        // If the discount was intended to be applied but the cart subtotal is too low
        return false; // Prevent it
    }

    // If $current was false to begin with, we respect that and return false.
    return $current;
}

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:252

public function woocommerce_subscriptions_is_recurring_fee_func( $current, $fee, $cart ) {
		if ( ! WC_Subscriptions_Cart::cart_contains_subscription() ) {
			return $current;
		}
		if ( isset( $fee->name ) && strpos( $fee->name, 'bulk discount' ) && $this->maybe_apply_recurring_discount( $cart ) ) {
			return apply_filters( 'ulgm_recurring_bulk_discount', true, $current, $fee, $cart );
		}

		return $current;
	}

Scroll to Top