Okay
  Public Ticket #2133870
Hide price for guest user in woocommerce
Closed

Comments

  •  2
    planet99 started the conversation

    hi,

    I want to hide the price for guest user in woocommerce. i only want to show the price for logged in user. i tried few plugin but it does not work with your theme. some places still showing either the price or add to cart button. can you offer a custom code for it?

  • [deleted] replied

    Hi planet99,

    Sorry but this is a feature that is not offered by WooCommerce or our theme. We have the option to disable prices and add to-cart so you can use your Website as catalog mode, but we don't any option to hide prices and show them for the logged in users only.

    Maybe there exist any plugin that can do this job.

    P.S If you're looking for professional help in this matter, there's always someone available for such tasks on the WP Kraken or Codeable. Pricing and timeline are always straightforward there and that's the main reason for recommending those services.

    Have a nice day! 

  •  2
    planet99 replied

    Hi,

    Can you guide me on the codes/shortcode that u use for displaying the prices/add to cart button. i see u are not using the woocommerce own code but your own. then i could put in few codes in the child function.php to hide the price.

    thanks

  • [deleted] replied

    Hi planet99,

    You can add this code in functions.php (end of file):

    // Hide price and add to cart for guest users
    add_action( 'woocommerce_init', function() {
        if ( ! is_user_logged_in() ) {
            add_filter( 'woocommerce_product_get_price', '__return_empty_string' );
        }
    } );
    
    It will hide price and add to cart functionality for guest users.

  •  2
    planet99 replied

    great! it is working. just one more thing. can i add a button that redirect to certain url? like 'please register to view pricing' button and then redirect to registration page? it will only show if the user is not login.


    thanks

  • [deleted] replied

    Hi planet99,

    Yes you can add it but with custom code. Can you tell me where do you want to show that text so I can help you with that.

  •  2
    planet99 replied

    Hi,

    1) Basically I want to show the registration button on all single product page. This would replace the add to cart button. I do not wish non member to see the price and add to cart button. instead they need to create an account to see the price.

    2) Your code previously work well but I notice that if I have variable product, the price range will show up for non member. Maybe additional code needed to hide this?

    Thanks

  • [deleted] replied

    Hi planet99,

    I have modified the previous code I gave you to fix the issue with variable products, and it also shows the button for non logged users:

    // Hide price and add to cart for guest users
    add_action( 'woocommerce_init', function() {
        if ( ! is_user_logged_in() ) {
            add_filter( 'woocommerce_product_get_price', '__return_empty_string' );
            add_filter( 'woocommerce_available_variation', '__return_empty_string' );
            add_filter( 'woocommerce_get_price_html', '__return_empty_string' );
            add_action( 'woocommerce_single_product_summary', 'kalium_register_button_for_non_customers', 30 );
            add_action( 'wp_footer', function() {
                echo '<style>.variations_form .stock.out-of-stock {display: none;}</style>';
            } );
        }
    } );
    function kalium_register_button_for_non_customers() {
        printf( '<a href="%s" class="button">Register to view price</a>', wc_get_page_permalink( 'myaccount' ) );
    }
    

    And it now works on variable products as well:

    medium
    (view large image)


    I hope you like it.

  •  2
    planet99 replied

    Hi,

    I think it does not working correctly. Please see the link. I can still see the add to cart button. Although the register button does appear just after the add to cart button.

    https://commercial.planetlighting.co.uk/product/12w-120-led-24v-ultra-warm-white-strip/

  • [deleted] replied

    Hi planet99,

    Can you please give us your WP credentials so I can test this code and make sure to hide add to cart for non logged in customers.

    To include your credentials click Insert Credentials button in editor toolbar. (See how)

    Your information is safe here (private) in this thread.

  •   planet99 replied privately
  • [deleted] replied

    Hi planet99,

    I have modified the code to work with variable products as well:

    // Hide price and add to cart for guest users
    add_action( 'woocommerce_init', function() {
        if ( ! is_user_logged_in() ) {
            add_filter( 'woocommerce_product_get_price', '__return_empty_string' );
            add_filter( 'woocommerce_available_variation', '__return_empty_string' );
            add_filter( 'woocommerce_get_price_html', '__return_empty_string' );
            add_action( 'woocommerce_single_product_summary', 'kalium_register_button_for_non_customers', 30 );
            add_action( 'wp_footer', function() {
                echo '<style>.variations_form .stock.out-of-stock {display: none;}</style>';
            } );
            
            remove_action( 'woocommerce_single_product_summary', 'kalium_woocommerce_catalog_mode_add_to_cart_options', 30 );
        }
    }, 100 );
    function kalium_register_button_for_non_customers() {
        printf( '<a href="%s" class="button">Register to view price</a>', wc_get_page_permalink( 'myaccount' ) );
    }
    // End of: Hide price and add to cart for guest users
    
    Result:

    medium
    (view large image)

    I hope this works for you.

  •  2
    planet99 replied

    Thanks! Work perfect.

  • [deleted] replied

    Glad to hear that planet99, wishing you a great week!

    If you like our theme, it would help us a lot if you could give us five star rating on themeforest from your Downloads tab. 

     A huge thank you from Laborator in advance!