, ,

Woocommerce – Hide price for non logged in users

Ever wanted to hide price for non logged in users in WooCommerce? try this code in you functions.php, now the price will be only visible for logged in users.

add_action('init', 'remove_price_ifnotloggedin');
function remove_price_ifnotloggedin() {
	if ( !is_user_logged_in() ) {
		remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 ); //hide price in shop page
		add_action( 'woocommerce_after_shop_loop_item_title', 'price_msg', 10 ); //add price message
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 ); //hide price in single product page page
		add_action( 'woocommerce_single_product_summary', 'price_msg', 10 ); //add price message
	}
}

function price_message() {
	echo '<span>Please login to see price!</span>';
}