Skip to content

Instantly share code, notes, and snippets.

@paaljoachim
Last active February 28, 2024 21:41
Show Gist options
  • Select an option

  • Save paaljoachim/4dd1d8013e20a660ab80a92b237aa009 to your computer and use it in GitHub Desktop.

Select an option

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.
<?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