Tiered and role-based prices in WooCommerce without a pricing plugin
Two pricing rules come up in almost every B2B or mixed shop: wholesale customers pay less, and larger carts get a discount. Dynamic pricing plugins cover hundreds of rule types; for these two, a few hooks are enough. The important decision is where each rule belongs: a customer-specific price changes the product price that is displayed and added to the cart, a cart discount is a negative fee or coupon that appears as its own line.
Problem: A shop sells to consumers and to resellers. Resellers with the role wholesale should see and pay 20% less on all products, and every cart over 100 € should get 10% off. A coupon-based workaround fails because customers can remove it and it stacks wrongly with other coupons.
Solution: Lower the product price for wholesale users with the woocommerce_product_get_price family of filters (so catalog, cart and order all use it, and the price cache varies per role), and add a negative cart fee for the tier discount in woocommerce_cart_calculate_fees, computed from the cart subtotal.
Role-based prices
function ha_is_wholesale(): bool {
return is_user_logged_in() && in_array( 'wholesale', (array) wp_get_current_user()->roles, true );
}
function ha_wholesale_price( $price, $product ) {
if ( '' === $price || ! ha_is_wholesale() ) {
return $price;
}
return (string) round( (float) $price * 0.8, wc_get_price_decimals() );
}
add_filter( 'woocommerce_product_get_price', 'ha_wholesale_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'ha_wholesale_price', 10, 2 );
// Variable products cache their price ranges; include the role in the cache key.
add_filter( 'woocommerce_get_variation_prices_hash', function ( array $hash ): array {
$hash[] = ha_is_wholesale() ? 'wholesale' : 'retail';
return $hash;
} );
Filtering the price getter means every place that asks the product for its price gets the wholesale price: the catalog, the product page, the cart and the order. Regular and sale prices stay unchanged, so the price display does not show a “sale” for wholesale users. The variation price hash filter is necessary because WooCommerce caches the min/max prices of variable products in a transient; without the role in the hash, wholesale users would see retail ranges or the other way around.
Page caches must not serve cached pages to logged-in users anyway; with role-based prices this is essential. Restricting whole products or the shop to roles works with the same role check: restrict WooCommerce products by user role.
The tier discount on the cart
add_action( 'woocommerce_cart_calculate_fees', function ( WC_Cart $cart ): void {
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
$subtotal = (float) $cart->get_subtotal(); // excluding tax and shipping, after wholesale prices
$tiers = array( 250 => 0.15, 100 => 0.10 ); // threshold => rate, highest first
foreach ( $tiers as $threshold => $rate ) {
if ( $subtotal >= $threshold ) {
$cart->add_fee(
/* translators: %d: discount percentage */
sprintf( __( 'Volume discount (%d%%)', 'textdomain' ), $rate * 100 ),
-1 * round( $subtotal * $rate, wc_get_price_decimals() ),
true // taxable: reduces the tax base proportionally
);
break;
}
}
} );
woocommerce_cart_calculate_fees runs every time the totals are calculated, so the discount follows changes in the cart automatically and cannot be removed by the customer. A negative fee appears as its own line in the cart, checkout, emails and the order, which makes the discount transparent. With true as taxable flag, tax is calculated on the reduced amount; the exact tax behavior of negative fees depends on the shop’s tax settings, so check an order with tax before going live.
Show the tier as an incentive
add_action( 'woocommerce_before_cart_totals', function (): void {
$missing = 100 - (float) WC()->cart->get_subtotal();
if ( $missing > 0 ) {
wc_print_notice( sprintf(
/* translators: %s: amount */
__( 'Add %s more to get 10%% off your order.', 'textdomain' ),
wc_price( $missing )
), 'notice' );
}
} );
NOTE: The Cart and Checkout blocks use the same cart calculation, so the fee works there too; the notice hook above is for the classic cart template, and the blocks need a Store API extension or a block for such messages. Fees on order totals in the context of the order lifecycle: WooCommerce order status transitions.