Filter uncanny-learndash-groups

ulgm_force_calculate_license_price

Filters to force recalculation of license price for a specific product.

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

Description

This filter hook allows developers to force a recalculation of the license price for a product. By returning `true`, you can trigger the `ulgm_force_new_license_price` filter. This is useful for custom pricing scenarios or when modifying product quantities that might affect license costs. It fires within the WooCommerce product modification logic.


Usage

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

Parameters

$product_id (mixed)
This parameter is a boolean value used to determine whether the license price should be forcefully recalculated, defaulting to false.

Return Value

The filtered value.


Examples

/**
 * Example: Force a specific license price calculation for a product.
 *
 * This callback function demonstrates how to force the calculation of a license price
 * for a specific product, potentially overriding default WooCommerce pricing logic.
 * In this example, if the product ID is '123', we'll force the license price
 * to be $50.00, regardless of other pricing rules.
 *
 * @param bool   $force_calculation Whether to force the license price calculation.
 * @param int    $product_id        The ID of the WooCommerce product.
 * @return bool                     True to force the calculation, false otherwise.
 */
add_filter( 'ulgm_force_calculate_license_price', function( $force_calculation, $product_id ) {
    // Check if we are specifically targeting product ID '123'
    if ( $product_id === 123 ) {
        // Return true to indicate that we want to force the license price calculation
        // for this specific product. The 'ulgm_force_new_license_price' filter will then
        // be used to set the actual price.
        return true;
    }

    // For all other products, revert to the default behavior by returning the original value.
    return $force_calculation;
}, 10, 2 );

/**
 * Example: Set a custom license price when calculation is forced.
 *
 * This callback function provides a custom price when the 'ulgm_force_calculate_license_price'
 * filter returns true for a product. Here, if the product ID is '123' (matching the previous
 * filter's condition), we'll set the price to $50.00.
 *
 * @param float       $current_price The current calculated price.
 * @param WC_Product  $product       The WooCommerce product object.
 * @return float                     The new license price.
 */
add_filter( 'ulgm_force_new_license_price', function( $current_price, $product ) {
    // Ensure we are working with the correct product ID.
    $product_id = $product->get_id();

    if ( $product_id === 123 ) {
        // Set the custom license price to $50.00.
        return 50.00;
    }

    // If it's not product ID '123', return the original calculated price.
    return $current_price;
}, 10, 2 );

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-modify-group.php:277

'new_qty'       => $qty,
		);

		if ( ulgm_filter_has_var( 'is_subscription_group' ) && 'yes' === ulgm_filter_input( 'is_subscription_group' ) ) {
			$save_data['is_subscription_group'] = 'yes';
		}

		if ( $product instanceof WC_Product && true === apply_filters( 'ulgm_force_calculate_license_price', false, $product_id ) ) {
			$price = apply_filters( 'ulgm_force_new_license_price', SharedFunctions::get_custom_product_price( $product, true ), $product );
			$product->set_price( $price );
			$product->set_regular_price( $price );
			$product->save();
			WoocommerceBuyCourses::clear_caches( $product );
		}


Scroll to Top