woocommerce_before_calculate_totals
WooCommerce fires the woocommerce_before_calculate_totals action inside WC_Cart::calculate_totals(), before it calculates totals for a nonempty cart. The callback receives the current $cart (WC_Cart) object and can inspect or change the product objects attached to cart items. It does not return a value.
Set a fixed unit price for a cart product
add_action( 'woocommerce_before_calculate_totals', function ( $cart ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
foreach ( $cart->get_cart() as $item ) {
if ( 123 === (int) $item['product_id'] ) {
$item['data']->set_price( 19.00 );
}
}
} );
Replace product ID 123 and price 19.00 with values suitable for your store. The callback sets the cart item's unit price to a fixed value rather than repeatedly discounting its current price. It does not change the product's stored catalog price. Use a site-specific plugin or child theme's functions.php, and test the actual totals on a staging store.
Repeated calculations and product variations
WooCommerce may recalculate cart totals more than once as a customer changes quantities, shipping details, or checkout choices. A percentage reduction based on an already modified get_price() value can compound unexpectedly. Setting an absolute price avoids that particular problem. Do not call $cart->calculate_totals() from this callback, because that would re-enter the same calculation.
For variations, $item['product_id'] is the parent product ID and $item['variation_id'] identifies the selected variation. This example therefore affects every variation of product 123. The hook is part of cart calculations rather than checkout-page presentation; check interactions with tax rules, coupons, and subscription or pricing extensions before using it for real orders. See the WooCommerce cart class for its execution point.