ulgm_get_course_price
Filters the price of a course before it is displayed or processed.
add_filter( 'ulgm_get_course_price', $callback, 10, 2 );
Description
Filter the price of a course product before it's displayed. This hook is useful for modifying course pricing based on custom logic or external data. It fires when retrieving the course price and receives the product object and an optional initial flag.
Usage
add_filter( 'ulgm_get_course_price', 'your_function_name', 10, 2 );
Parameters
-
$price(mixed) - This parameter holds the current price of the course, which can be modified by the filter.
-
$product(mixed) - This parameter holds the current price of the course being fetched.
Return Value
The filtered value.
Examples
/**
* Example of using the 'ulgm_get_course_price' filter to dynamically adjust course prices.
*
* This function demonstrates how to modify the price of a course based on
* user roles or other custom logic before it's displayed.
*
* @param float|string $price The current price of the course.
* @param WC_Product $product The WooCommerce product object for the course.
* @return float|string The modified course price.
*/
add_filter( 'ulgm_get_course_price', function( $price, $product ) {
// Check if the current user is logged in and has a specific role.
if ( is_user_logged_in() && current_user_can( 'premium_member' ) ) {
// Apply a discount for premium members.
$discount_percentage = 10; // 10% discount
$discount_amount = $price * ( $discount_percentage / 100 );
$modified_price = $price - $discount_amount;
// Ensure the price doesn't go below zero.
return max( 0, $modified_price );
}
// If no special conditions are met, return the original price.
return $price;
}, 10, 2 ); // Priority 10, accepts 2 arguments: $price and $product
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:93
public function get_custom_product_price( $product, $initial = false ) {
if ( $product instanceof WC_Product && $product->is_type( 'courses' ) ) {
if ( 'yes' === (string) get_option( 'woocommerce_calc_taxes' ) ) {
$price = 'yes' === get_option( 'woocommerce_prices_include_tax' ) ? wc_get_price_including_tax( $product ) : wc_get_price_excluding_tax( $product );
} else {
$price = $product->get_price();
}
WoocommerceLicense::$product_price[ $product->get_id() ] = $price;
return apply_filters( 'ulgm_get_course_price', $price, $product );
}
if ( $product instanceof WC_Product && $product->is_type( 'license' ) ) {
return SharedFunctions::get_license_price( $product, $initial );
}
return $product->get_price();
}