Menu Close

WooCommerce: Hide Price & Add to Cart for Logged Out Users

default

You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out.

All you need is pasting the following code in your functions.php (please note: your theme may have overwritten some default WooCommerce functions, hence the code below may not work. Contact me if you need custom code). Enjoy!

woocommerce-hide-prices-add-cart-shop-single-product
WooCommerce: Hide Price & Add to Cart to Logged Out Users

PHP Snippet: Hide Add to Cart Buttons and Prices if Logged Out @ WooCommerce Shop, Single Product Pages, Widgets, etc.

/**
 * @snippet       Hide Price & Add to Cart for Logged Out Users
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, BusinessBloomer.com
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_get_price_html', 'bbloomer_hide_price_addcart_not_logged_in', 9999, 2 );

function bbloomer_hide_price_addcart_not_logged_in( $price, $product ) {
	if ( ! is_user_logged_in() ) { 
		$price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'bbloomer' ) . '</a></div>';
		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 );
      add_filter( 'woocommerce_is_purchasable', '__return_false' );
	}
	return $price;
}
View Source
Posted in WooCommerce Tips