Last active
February 28, 2024 21:41
-
-
Save paaljoachim/4dd1d8013e20a660ab80a92b237aa009 to your computer and use it in GitHub Desktop.
WooCommerce show/hide products based on if the user is logged inn or logged out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // https://businessbloomer.com/woocommerce-remove-specific-category-shop-loop/ | |
| // https://stackoverflow.com/questions/34684881/hide-products-from-users-who-are-not-logged-in-using-tags/34689768#34689768 | |
| add_action( 'woocommerce_product_query', 'show_hide_products_category_shop' ); | |
| function show_hide_products_category_shop( $q ) { | |
| $tax_query = (array) $q->get( 'tax_query' ); | |
| if ( is_user_logged_in() ) { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_cat', | |
| 'field' => 'slug', | |
| 'terms' => array( 'medlem' ), // Category slug here | |
| 'operator' => 'NOT IN' | |
| ); | |
| } else { | |
| $tax_query[] = array( | |
| 'taxonomy' => 'product_cat', | |
| 'field' => 'slug', | |
| 'terms' => array( 'ukens pose' ), // Category slug here | |
| 'operator' => 'NOT IN' | |
| ); | |
| } | |
| $q->set( 'tax_query', $tax_query ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment