ulgm_get_license_price
Filters the license price before it's displayed, allowing modification of pricing based on product details.
add_filter( 'ulgm_get_license_price', $callback, 10, 2 );
Description
Filters the license price for a WooCommerce product. Developers can modify the price before it's displayed or used in calculations, allowing for custom pricing logic, discounts, or dynamic adjustments based on product details.
Usage
add_filter( 'ulgm_get_license_price', 'your_function_name', 10, 2 );
Parameters
-
$price(mixed) - This parameter contains the current price of the license, which can be modified by the filter.
-
$product(mixed) - This parameter represents the current price of the license being retrieved, which can be filtered by other plugins.
Return Value
The filtered value.
Examples
/**
* Example of using the 'ulgm_get_license_price' filter to modify the license price.
*
* This example demonstrates how to dynamically adjust the license price based on
* whether the product is a "variable" product and has specific meta data.
*
* @param float $price The current calculated license price.
* @param WC_Product $product The WC_Product object for which the price is being calculated.
* @return float The modified license price.
*/
add_filter( 'ulgm_get_license_price', function( $price, $product ) {
// Check if it's a variable product and if it has a custom price meta field.
if ( $product->is_type( 'variable' ) && $product->meta_exists( '_custom_variable_license_price' ) ) {
$custom_price = $product->get_meta( '_custom_variable_license_price', true );
// If a custom price is set and it's a valid positive float, use it.
if ( ! empty( $custom_price ) && is_numeric( $custom_price ) && $custom_price > 0 ) {
return floatval( $custom_price );
}
}
// If no custom price is found or applicable, return the original price.
return $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/handlers/class-woocommerce-handlers.php:156
public function get_license_price( WC_Product $product, $initial = false, $current_price = 0 ) {
$price = 0;
$custom = 0;
if ( ! $product instanceof WC_Product ) {
return $price;
}
if ( ! $product->is_type( 'license' ) ) {
return $product->get_price();
}
if ( ! $initial ) {
if ( empty( $current_price ) || 0 === $current_price ) {
$custom = 0;
$initial = true;
} else {
$price = $current_price;
$custom = 1;
}
}
if ( 1 !== absint( $custom ) || true === $initial ) {
$linked_courses = get_post_meta( $product->get_id(), SharedFunctions::$license_meta_field, true );
$new_courses = get_post_meta( $product->get_id(), SharedFunctions::$license_meta_field . '_new', true );
if ( ! empty( $new_courses ) ) {
$linked_courses = $new_courses;
}
if ( $linked_courses ) {
foreach ( $linked_courses as $course ) {
// Prevent recursion: if course product is the same as the license product, skip it
if ( (int) $course === (int) $product->get_id() ) {
continue;
}
if ( false !== get_post_status( $course ) ) {
$course_product = wc_get_product( $course );
if ( ! ( $course_product instanceof WC_Product ) ) {
continue;
}
$price_wt = SharedFunctions::get_custom_product_price( $course_product );
$price = floatval( $price ) + floatval( $price_wt );
}
}
}
}
return apply_filters( 'ulgm_get_license_price', $price, $product );
}