Support » Plugin: WooCommerce » Hide prices and checkout functionality for specific product category
Hide prices and checkout functionality for specific product category
-
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
-
You can do this by using the Catalog Visibility Options plugin.
Note: That plugin acts on the entire catalog, not just one product or one category.
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()
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!
[edited]
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; }
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 🙂
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.
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.
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.could you please help?
Thanks.
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.
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?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.
It worked! 😀
Thank you so much javierllinas.
Appreciate your help. 🙂
- The topic ‘Hide prices and checkout functionality for specific product category’ is closed to new replies.