Sarathlal N

Display product price for logged in users only - WooCommerce

In a recent WooCommerce customization, our client ask to hide product price from public. Every one can see the product details. But the price is only visible for logged in users only. We just want to use a simple code snippet in our theme’s / child theme’s functions.php file to achieve this functionality.

add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
if(is_user_logged_in() ){
	return $price;
}
else return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to view price!';
}

The above code snippet will show price for logged in users and a link to login / register for not logged in users.

Recent Posts

  1. Automating Release Generation with GitHub Actions
  2. WP CLI Commands to Bulk Delete Entries in WordPress Database
  3. Split a Single CSV File into Multiple Files Using the Split Command - Bash
  4. Migrating code repo from BitBucket to GitHub
  5. Streamlining Development - Our Journey with Git, Bitbucket, and Jira

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.