Back to Snippets

WooCommerce: Discount for Logged-In Users

PHP WooCommerce April 6, 2026

Apply automatic percentage discount for logged-in WooCommerce customers with a shop page notice. Handles admin and product type checks.

Snippet Stats

Lines 31
Characters 850
Read 1 min
php • 31 lines
/**
 * Apply a 10% member discount for logged-in users.
 *
 * @param string     $price   The current price.
 * @param WC_Product $product The product object.
 * @return string
 */
add_filter( 'woocommerce_product_get_price', function( string $price, WC_Product $product ): string {
    if ( ! is_user_logged_in() || is_admin() ) {
        return $price;
    }

    // Only apply to simple products.
    if ( 'simple' !== $product->get_type() ) {
        return $price;
    }

    return (string) round( (float) $price * 0.9, 2 );
}, 10, 2 );

/**
 * Show a notice about the member discount.
 */
add_action( 'woocommerce_before_shop_loop', function(): void {
    if ( is_user_logged_in() ) {
        wc_print_notice(
            __( 'You are getting a 10% member discount on all products!', 'theme-domain' ),
            'notice'
        );
    }
} );

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This React Higher Order Component handles authentication and authorization by restricting access to components based on user roles. It utilizes React ...

React Higher Order Component For Role-Based Authentication And Authorization

PHP
Add a validated custom checkout field to WooCommerce billing — includes field rendering, validation, and order meta storage.

WooCommerce: Add Custom Checkout Field