This React Higher Order Component handles authentication and authorization by restricting access to components based on user roles. It utilizes React ...
Back to Snippets
php
• 31 lines
WooCommerce: Discount for Logged-In Users
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
/**
* 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 →