Hide Prices for Non-Logged-In Users in WooCommerce

How to hide prices for non-logged-in users in woocommerce; Woocommerce hide prices until login; Hide product prices for guests in wordpress; Restrict price visibility to logged-in users woocommerce; Woocommerce show prices only to logged-in users; Hide prices from non-members in woocommerce; Wordpress hide prices for non-logged-in users; Woocommerce restrict price display to registered users; How to make prices visible only after login in woocommerce; Hide 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

<?php
// Hook to WooCommerce to modify the price display
add_filter('woocommerce_get_price_html', 'wp_dudecom_hide_price_for_non_logged_in_users', 10, 2);

/**
 * Hide product prices for non-logged-in users in WooCommerce.
 *
 * @param string $price The original price HTML.
 * @param object $product The WooCommerce product object.
 * @return string Modified price HTML.
 */
function wp_dudecom_hide_price_for_non_logged_in_users($price, $product) {
    // Check if the user is not logged in
    if (!is_user_logged_in()) {
        // Return a custom message instead of the price
        return __('Login to see prices', 'woocommerce');
    }
    // Return the original price for logged-in users
    return $price;
}

// Hook to WooCommerce to modify the add to cart button
add_filter('woocommerce_loop_add_to_cart_link', 'wp_dudecom_modify_add_to_cart_button_for_non_logged_in_users', 10, 2);

/**
 * Modify the add to cart button for non-logged-in users in WooCommerce.
 *
 * @param string $button The original add to cart button HTML.
 * @param object $product The WooCommerce product object.
 * @return string Modified add to cart button HTML.
 */
function wp_dudecom_modify_add_to_cart_button_for_non_logged_in_users($button, $product) {
    // Check if the user is not logged in
    if (!is_user_logged_in()) {
        // Return a custom message instead of the add to cart button
        return '<a href="' . esc_url(wp_login_url(get_permalink($product->get_id()))) . '" class="button">' . __('Login to purchase', 'woocommerce') . '</a>';
    }
    // Return the original button for logged-in users
    return $button;
}
?>

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.