woocommerce_variation_prices_price
Filters the active price for a product variation before it is used in price calculations and caching.
IMPORTANT: If your filter returns a price that varies by ANY factor (e.g., current user, time of day, user role, cart contents, etc.), you MUST also hook into the woocommerce_get_variation_prices_hash to include that factor in the cache key. Otherwise, the wrong price may be cached and displayed to different users or contexts.
Example: Applying a per-user discount and adapting the price hash info accordingly:
class My_Custom_Pricing {
public function __construct() {
add_filter( 'woocommerce_variation_prices_price', array( $this, 'apply_user_discount' ), 10, 3 );
add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_user_to_hash' ), 10, 3 );
}
public function apply_user_discount( $price, $variation, $product ) {
return $price * $this->get_discount_for_user( get_current_user_id() );
}
public function add_user_to_hash( $price_hash, $product, $for_display ) {
$price_hash[] = get_current_user_id();
return $price_hash;
}
}
Usage
add_filter( 'woocommerce_variation_prices_price', 'wp_kama_woocommerce_variation_prices_price_filter', 10, 3 );
/**
* Function for `woocommerce_variation_prices_price` filter-hook.
*
* @param string|float $price The variation's active price.
* @param WC_Product $variation The variation product object.
* @param WC_Product $product The parent variable product object.
*
* @return string|float
*/
function wp_kama_woocommerce_variation_prices_price_filter( $price, $variation, $product ){
// filter...
return $price;
}
- $price(string|float)
- The variation's active price.
- $variation(WC_Product)
- The variation product object.
- $product(WC_Product)
- The parent variable product object.
Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
woocommerce_variation_prices_price
woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php 383
$price = apply_filters( 'woocommerce_variation_prices_price', $variation->get_price( 'edit' ), $variation, $product );