Woocommerce: Disable Prices Unless User is Logged In

I’m using this code to disable prices unless the user is logged in to see them:

add_action('init', 'bbloomer_hide_price_add_cart_not_logged_in');
 
function bbloomer_hide_price_add_cart_not_logged_in() { 
if ( !is_user_logged_in() ) {       
 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', 'bbloomer_print_login_to_see', 31 );
 add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}
 
function bbloomer_print_login_to_see() {
echo '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">' . __('Login to see prices', 'dvkap') . '</a>';
}

The downside is that it does not disable prices from the WishList/Cart and Checkout area. I want to completely disable prices from being visible unless the user is logged in. Any ideas?

I’ve not looked in the cart/checkout area in a long time, so I am not sure which is the code that displays the prices/total.

As a quick solution, you could hide them.

if(! is_user_logged_in() ) : ?>
   jQuery(document).ready(function() {
    jQuery(".product-price").hide();
});
<?php endif; ?>

It’s not fancy, nor completely safe (since it only avoids the display, but leaves the code there. Plus if it’s a large site you may increase the render time and such. But it could help you, again as a quick fix.
I’ll try to make some time to review an install I did with a Woocommerce plugin to see how it works (and possibly give you a better/safer solution).

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.