Hide Price & Add to Cart for Non Logged in Users

Solutions:

To hide the price and “Add to Cart” button for non-logged-in users on specific products in WordPress, you can use the following code snippet. This code should be added to your theme’s functions.php file or in a custom plugin.

/**
 * Snippet Name: To hide the price and "Add to Cart" button for non-logged-in users on specific products
 * Snippet Author:  https://wpproblog.com
 */
//Hide Price & Add to Cart for Non Logged in Users for Specific Products
function hide_price_add_to_cart_non_logged_in_users() {
    if ( ! is_user_logged_in() ) {
        global $product;
        
        // Define the product IDs for which you want to hide price and "Add to Cart"
        $product_ids = array( 1, 2, 3 ); // Replace with your desired product IDs
        
        // Check if the current product is in the defined list
        if ( in_array( $product->get_id(), $product_ids ) ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            
            // Optionally, you can also add a message or custom text instead of the price
            add_action( 'woocommerce_single_product_summary', 'custom_non_logged_in_user_message', 10 );
        }
    }
}
add_action( 'woocommerce_before_single_product', 'hide_price_add_to_cart_non_logged_in_users' );

function custom_non_logged_in_user_message() {
    echo '<p>Please log in to view the price and purchase this product.</p>';
}

Output:

The hide_price_add_to_cart_non_logged_in_users function is hooked to the woocommerce_before_single_product action. It checks if the user is not logged in and if the current product ID matches the defined list of product IDs.

Modify the $product_ids array to include the specific product IDs for which you want to hide the price and “Add to Cart” button. Update the array with the desired product IDs.

To hide the price and “Add to Cart” button for non-logged-in users on specific products within a category in WordPress, you can use the following code snippet:

/**
 * Snippet Name: To hide the price and "Add to Cart" button for non-logged-in users on specific products
 * Snippet Author:  https://wpproblog.com
 */
//Hide Price & Add to Cart for Non Logged in Users for Specific Category
function hide_price_add_to_cart_non_logged_in_users_category( $is_visible, $id ) {
    if ( ! is_user_logged_in() ) {
        $product_categories = array( 'category-slug' ); // Replace 'category-slug' with the slug of the desired category
        $product_id = $id;

        // Check if the current product belongs to the specified category
        if ( has_term( $product_categories, 'product_cat', $product_id ) ) {
            $is_visible = false;
        }
    }
    return $is_visible;
}
add_filter( 'woocommerce_variation_is_visible', 'hide_price_add_to_cart_non_logged_in_users_category', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'hide_price_add_to_cart_non_logged_in_users_category', 10, 2 );

Make sure to place this code snippet in your theme’s functions.php file or in a custom plugin.