Snippet

Hide Prices for Non-Logged-In Users in WooCommerce

How to hide prices for non-logged-in users in woocommerceWoocommerce hide prices until loginHide product prices for guests in wordpressRestrict price visibility to logged-in users woocommerceWoocommerce show prices only to logged-in usersHide prices from non-members in woocommerceWordpress hide prices for non-logged-in usersWoocommerce restrict price display to registered usersHow to make prices visible only after login in woocommerceHide prices for non-logged-in users wordpress

Explanation

If you're running a WooCommerce store and want to keep your prices a secret from non-logged-in users, this code is just what you need. It cleverly hides the prices and changes the "Add to Cart" button for those who aren't logged in.

Here's how it works:

  • Hide Prices: The code checks if a user is logged in. If they're not, instead of showing the price, it displays a friendly message saying, "Login to see prices." This encourages visitors to log in or register to view the prices.
  • Modify Add to Cart Button: For users who aren't logged in, the usual "Add to Cart" button is replaced with a link that says "Login to purchase." This link takes them to the login page, making it easy for them to sign in and proceed with their shopping.

This approach helps you manage who can see your prices and purchase products, ensuring that only registered users have full access to your store's offerings.

Code

1<?php 2// Hook to WooCommerce to modify the price display 3add_filter('woocommerce_get_price_html', 'wp_dudecom_hide_price_for_non_logged_in_users', 10, 2); 4 5/** 6 * Hide product prices for non-logged-in users in WooCommerce. 7 * 8 * @param string $price The original price HTML. 9 * @param object $product The WooCommerce product object. 10 * @return string Modified price HTML. 11 */ 12function wp_dudecom_hide_price_for_non_logged_in_users($price, $product) { 13 // Check if the user is not logged in 14 if (!is_user_logged_in()) { 15 // Return a custom message instead of the price 16 return __('Login to see prices', 'woocommerce'); 17 } 18 // Return the original price for logged-in users 19 return $price; 20} 21 22// Hook to WooCommerce to modify the add to cart button 23add_filter('woocommerce_loop_add_to_cart_link', 'wp_dudecom_modify_add_to_cart_button_for_non_logged_in_users', 10, 2); 24 25/** 26 * Modify the add to cart button for non-logged-in users in WooCommerce. 27 * 28 * @param string $button The original add to cart button HTML. 29 * @param object $product The WooCommerce product object. 30 * @return string Modified add to cart button HTML. 31 */ 32function wp_dudecom_modify_add_to_cart_button_for_non_logged_in_users($button, $product) { 33 // Check if the user is not logged in 34 if (!is_user_logged_in()) { 35 // Return a custom message instead of the add to cart button 36 return '<a href="' . esc_url(wp_login_url(get_permalink($product->get_id()))) . '" class="button">' . __('Login to purchase', 'woocommerce') . '</a>'; 37 } 38 // Return the original button for logged-in users 39 return $button; 40} 41?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme Editor if you are adding the code to functions.php. Alternatively, navigate to Plugins > Editor if you are using a custom plugin.
  3. In the Theme Editor, locate and select the functions.php file from the list on the right. If using a plugin, select the appropriate plugin file.
  4. Copy and paste the provided code snippet into the file.
  5. Click Update File to save your changes.
  6. Visit your WooCommerce store as a non-logged-in user to verify that prices are hidden and the "Add to Cart" button is modified as intended.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.