WordPress.org

Forums

WooCommerce - excelling eCommerce
Hide prices and checkout functionality for specific product category (14 posts)

  1. javierllinas
    Member
    Posted 1 year ago #

    Hello!

    One of the brands my client is going to market, forces her to require registration/login to show prices: we need to hide the prices of an entire woocommerce product category for non logged users.

    This thread helped me to hide all the prices, but I can't tweak that code to get just the specific product category.

    Thank you guys in advance,
    Javier

    https://wordpress.org/plugins/woocommerce/

  2. Daniel Espinoza
    Member
    Posted 1 year ago #

    You can do this by using the Catalog Visibility Options plugin.

  3. Daniel Espinoza
    Member
    Posted 1 year ago #

    Note: That plugin acts on the entire catalog, not just one product or one category.

  4. javierllinas
    Member
    Posted 1 year ago #

    Thank you, Daniel. But, as I've said, I don't want to hide all the products: just an specific category (or tag).

    This ain't working ("my-category-slug" is the category):

    add_filter('woocommerce_get_price_html','members_only_price');
    
    function members_only_price($price){
    
    	wp_reset_query(); 
    
    	global $post;
    	$terms = get_the_terms( $post->ID, 'product_cat' );
    	foreach ( $terms as $term ) $categories[] = $term->slug;
    
    		if ( in_array( 'my-category-slug', $categories ) ) {
    			remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    			remove_action( 'woocommerce_single_product_summary', 'woocommerce_single_price' );
    			remove_action( 'woocommerce_single_product_summary', 'woocommerce_add_to_cart' );
    			remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );
    			return 'To know our price<a class="button" href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Register</a>';
    		}
    
    		else {
    			return $price;
    		}
    
    }

    Of course, I need to wrap all of this within an if ! is_user_logged_in()

  5. Daniel Espinoza
    Member
    Posted 1 year ago #

    The plugin would give you a starting point and code that does work.

    After a cursory look at the code, I'm not sure you'll be able to remove_action() at the point the add_filter() executes. You may need to move it up in the order of execution. Best of luck!

  6. LandingOnJupiter
    Member
    Posted 1 year ago #

    [edited]

    @javierllinas

    This should work for a Single Product page. But, it won't work on Archive pages, because once an action is removed it's removed for the remainder of the page rendering.

    In my code below, if you remove the "is_singular" if statement and look at a mixed category archive page, you should see what I mean. As soon as the first product with the category "my-category-slug" is processed, all other items will no longer display a price.

    add_filter('woocommerce_get_price_html','members_only_price', "", 2);
    
    function members_only_price($price,$product){
    
    	if (is_singular(array('product'))) { // single product page
    		$terms = get_the_terms($product->ID, "product_cat");
    
    		foreach ( $terms as $term ) {
    			$categories[] = $term->slug;
    		}
    		if ( in_array( 'my-category-slug', $categories ) ) {
    							remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    			remove_action( 'woocommerce_single_product_summary', 'woocommerce_single_price' );
    			remove_action( 'woocommerce_single_product_summary', 'woocommerce_add_to_cart' );
    			remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price' );
    				return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
    		}
    	}
    
    	return $price;
    }
  7. javierllinas
    Member
    Posted 1 year ago #

    Thank you, @LandingOnJupiter

    You are right: it doesn't work on archive pages. What I did was add the actions again within an else statement.

    Thank you for your time :)

  8. LandingOnJupiter
    Member
    Posted 1 year ago #

    So, is it working for you?

    Part of your original issue is that the remove_action was missing the priority. So, they wouldn't be turned off.

    You had:
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );

    And it should be:
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

    Another issue is that if you remove woocommerce_template_loop_price, it will never re-run your filter. (Test it by putting an echo "hello"; at the beginning of your code and viewing an archive page.) But, you shouldn't need to remove it, since you're not returning the price in your function, anyway.

  9. javierllinas
    Member
    Posted 1 year ago #

    This works on archive and single product pages:

    add_action('woocommerce_get_price_html','members_only_price');
    
    function members_only_price($price){
    
    	global $post, $product;
    	$terms = get_the_terms( $product->ID, 'product_cat' );
    	foreach ( $terms as $term ) $categories[] = $term->slug;	
    
    		if ( ! is_user_logged_in() && in_array( 'my-category-slug', $categories ) ) {
    			remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2);
    			remove_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_template_single_product_add_to_cart', 10, 2);
    			remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    			remove_action( 'init', 'woocommerce_add_to_cart_action', 10);			
    
    			return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
    		}
    
    		else {
    			add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10, 2);
    			add_action( 'init', 'woocommerce_add_to_cart_action', 10);
    			add_action( 'init', 'woocommerce_checkout_action', 10 );
    			return $price;
    		}
    
    }

    May not be the smartest approach (I apologize to the php/woocommerce ninjas), but gets the job done.

  10. akash666
    Member
    Posted 4 months ago #

    Hello javierllinas!

    I'm using your code. Its working fine but the latest product (very last one) is showing add to cart button.
    Here you can see it.

    http://tinyurl.com/moawg4e

    could you please help?

    Thanks.

  11. javierllinas
    Member
    Posted 4 months ago #

    Hello akash666,

    From your example, I guess you mean first product.

    I'm afraid I don't know why is this happening.
    But notice that, when you switch to list view, the add-to-cart button is not showing anymore: there may be some conflict related with this functionality in the theme. Check the priority of the actions.

    As an easy workaround I suggest to hide the button via css. Something like:

    .product-cat-[here goes the category class] .add_to_cart_button{ display:none; }
    .logged-in .product-cat-[here goes the category class] .add_to_cart_button{ display:inline-block; }

    Hope this can somehow help.

  12. akash666
    Member
    Posted 4 months ago #

    Thank you for your response javierllinas.
    I'm sorry but I've no idea about Classes. is this something similar to slugs? or else where should I find it?

  13. javierllinas
    Member
    Posted 4 months ago #

    Woocommerce generates a css class for all product categories. This class is the same as the slug, preceded by product-cat-
    Also, WordPress has a logged-in class.

    In your case:

    .product-cat-drop-shipping .add_to_cart_button{ display:none !important; }
    .logged-in .product-cat-drop-shipping .add_to_cart_button{ display:inline-block !important; }

    Remember you're just tweaking the front (appearance): the button will still be in the code.

  14. akash666
    Member
    Posted 4 months ago #

    It worked! :D

    Thank you so much javierllinas.
    Appreciate your help. :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic