ulgm_force_new_license_price
Filters the price of a new license, allowing modifications before it's applied to the product.
add_filter( 'ulgm_force_new_license_price', $callback, 10, 2 );
Description
Filters the calculated license price for subscription groups. Use this hook to override or modify the default license price before it's saved, allowing for custom pricing logic based on product details. The hook receives the current calculated price and the WC_Product object.
Usage
add_filter( 'ulgm_force_new_license_price', 'your_function_name', 10, 2 );
Parameters
-
$product(mixed) - This parameter is the WooCommerce product object for which the license price might be forced to be recalculated.
-
$product(mixed) - This parameter is expected to contain a WooCommerce product object.
Return Value
The filtered value.
Examples
<?php
/**
* Example of using the 'ulgm_force_new_license_price' filter to dynamically
* adjust the license price for a WooCommerce product based on its ID.
*
* This example sets a fixed price of 99.99 for products with a specific ID,
* demonstrating how to override the default price calculation.
*
* @param float $current_price The current calculated license price.
* @param WC_Product $product The WooCommerce product object.
* @return float The modified license price.
*/
add_filter( 'ulgm_force_new_license_price', function( $current_price, $product ) {
// Define the product ID you want to apply the special price to.
$special_product_id = 123; // Replace with an actual product ID from your store.
// Check if the current product's ID matches the special product ID.
if ( $product->get_id() === $special_product_id ) {
// Set a new, fixed price for this specific product.
$new_price = 99.99;
// You can add more complex logic here, for example:
// if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {
// $new_price = 49.99; // A discounted price for admins.
// }
return (float) $new_price;
}
// If the product ID doesn't match, return the original calculated price.
return (float) $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:278
);
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 );
}
SharedFunctions::remove_transient_cache( 'no', '_ulgm_user_buy_courses_USERID_order', $user_id );