Hide Price & Add to Cart for Logged Out Users in Woocommerce

In order to hide Prices and Add to Cart for unregistered users on shop page and single product page and also show specific message “Login to see prices” instead of price or add to cart button on the single product page or Shop page. Let’s do this. Paste this code in your theme’s functions.php file of your active child theme (or theme).

Save the file and log out from admin panel to check the changes on the front-end:

add_action('init', 'wp_hide_price_add_cart_not_logged_in');
 
function wp_hide_price_add_cart_not_logged_in() { 
if ( !is_user_logged_in() ) {       
 remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
 remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );  
 add_action( 'woocommerce_single_product_summary', 'wp_print_login_to_see', 31 );
 add_action( 'woocommerce_after_shop_loop_item', 'wp_print_login_to_see', 11 );
}
}
 
function wp_print_login_to_see() {
echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>';
}

Leave a Comment

Scroll to Top