Hide prices of a certain product category to non logged users

Hello all!

I need to hide the prices of an entire product category, just if user is not logged.

One of the brands my client is going to market, forces her to require registration/login to show prices; its mandatory.
I need something like this guys are doing: shoprescuespa.com/shop-by-skin-type/dry.html -> as you can see, some products have a 'log in' button instead of 'add to cart'.

Also, I cannot see any registration/login built-in functionality within Marketpress, is there any?

Thank you all!

Javier

  • Charlie Pryor
    • The Incredible Code Injector

    Hello Jatstudio!

    You could use the Membership plugin alongside the Marketpress plugin for this functionality. You'd be able to isolate products of a specific category using Acess levels, and then make signed-in members of your site have access to that category, while anyone who doesn't have that access level (those who aren't logged in) wouldn't be able to see or interact with it.

    You can see a preview and tutorial series on the Membership Plugin by viewing the USAGE PAGE for the plugin here. You can view the whole video series to learn how to use the plugin, but the bread and butter of what I'm referring to here can be found within video #10 in the series

    Hope that helps!

    - Charlie

  • jatstudio
    • WPMU DEV Initiate

    I think I myself can build the login/registration system.
    But I can´t manage to get the product category, someting like:

    if ( !is_user_logged_in() && is_product_category('myCat')) {	
    
    			add_filter( 'mp_product_price_tag', create_function('', "return '';"));
    			add_filter( 'mp_buy_button_tag', create_function('', "return '';"));									
    
    		}

    How do I check if is a certain Marketpress product category ('myCat', in the example).

    Thank you in advance!

  • Charlie Pryor
    • The Incredible Code Injector

    To add to that advice, because I reread your question again and thought this might be useful too, you could allow anyone to view the products, but block access to the "Add to Cart button" with Memberships shortcodes. Surround the cart button with a shortcode, and then create a login button on the product page wrapped with another shortcode that made it only visible to non-logged in members.

    Basically, you'd hide the cart button for logged out users, and reveal the log in button. For logged in users, you'd reveal the cart button, and hide the login button.

    - Charlie

  • Charlie Pryor
    • The Incredible Code Injector

    Sorry I must have posted my last comment as you were leaving yours.

    What you're asking (PHP code help) is beyond what I know personally. You'll have to wait for staff there.

    If you're modifying theme files though, be careful about it. Any updates to the plugin (and there will be many) will likely erase your code.

    - Charlie

  • Patrick
    • Support Monkey

    Hi there @jatstudio

    Welcome to the forums, glad to have you aboard!

    You'll likely find just what you need by taking a look through marketpress/marketpress-includes/template-functions.php.

    All the functions that you can use in your theme templates are in there and are well documented.

  • jatstudio
    • WPMU DEV Initiate

    Hello, Patrick.

    I know that. I've used that file to make all the customisations (this is a client custom theme). Everythings seems to be up and running except for this.

    This is what I´m trying: ( I apologize to the php ninjas in advance )

    if ( !is_user_logged_in() ) {
    
    			$args = array(
    				'post_type' => 'product',
    				'tax_query' => array(
    					array(
    						'taxonomy' => 'product_category',
    						'field' => 'slug',
    						'terms' => 'my-slug' // the category I want to hide
    					)
    				)
    			);
    			$query = new WP_Query( $args );   		
    
    			if( $query ){
    
    			add_filter( 'mp_product_price_tag', create_function('', "return '';"));
    			add_filter( 'mp_buy_button_tag', create_function('', "return '';"));
    
    			}
    
    		}
  • jatstudio
    • WPMU DEV Initiate

    No guidance on this?

    This is what I tried without success

    //hide some product_category prices & buy buttons from non logged in visitors
    		if ( !is_user_logged_in() ) {
    
    			$rm_products =  get_post_types( '','product' );
    			if ( $rm_products ) {
    				foreach ( $rm_products as $rm_product ) {					
    
    					if ( has_term( 'my-product-category-slug' )  ) {
    
    						add_filter( 'mp_product_price_tag', create_function('', "return '';"));
    						add_filter( 'mp_buy_button_tag', create_function('', "return '';"));
    					}
    				}
    			}
    		}
  • Patrick
    • Support Monkey

    Hi again @jatstudio

    Whoops, I apologize for the delay on this one... the one that got away, I guess. :slight_smile:

    I'm unfortunately not one of the php ninjas around here, but I do know a few. I'll see if I can get one of them to pop in with a bit of insight for you. Hang on...

  • Jonathan
    • The Bug Hunter

    Hey @jatstudio,

    Try adding this to your theme's functions.php file:

    add_filter('the_content', 'my_hide_for_unauthorized_users', 1);
    function my_hide_for_unauthorized_users() {
    	if ( !is_user_logged_in() && has_term('my-product-category-slug', 'product_category') ) {
    		add_filter( 'mp_product_price_tag', create_function('$html, $post_id, $label', "return '';"));
    		add_filter( 'mp_buy_button_tag', create_function('$button, $post_id, $context', "return '';"));
    	}
    }
  • jatstudio
    • WPMU DEV Initiate

    Thank you, Jonathan.

    Unfortunately, this is not working for me.

    I finally opted for the front-end patch (css and jquery): adding a "not-logged class" to the body to hide the buttons and appending a log-in button via javascript.

    By the way, I'm using products tags (not categories), because categories don't output any class (at least, not in my case). With tags, the output (list view) is something like this:
    <div class="product-399 product type-product hentry tag-my-product-category-slug mp_product"[...]

    Hope this helps.

    Thanks for your time, guys.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.