Last active
November 3, 2017 08:28
-
-
Save andrewlimaza/6ee480694d38a99695503febe3cdabf1 to your computer and use it in GitHub Desktop.
Remove items from WooCommerce shop if user does not have PMPro membership level.
This file contains 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 | |
/** | |
* Remove items from WooCommerce shop loop if user does not have an active PMPro Membership Level. | |
* Tested on WooCommerce 3 and up. | |
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* Please note that this won't restrict products if accessed directly and will require further custom code to achieve. | |
* Visit https://paidmembershipspro.com for more info/assistance. | |
*/ | |
add_action( 'pre_get_posts', 'pmpro_remove_cat_from_shop' ); | |
function pmpro_remove_cat_from_shop( $q ) { | |
// Bail if user is admin/member and do not affect any posts inside WP admin. | |
if( ( pmpro_hasMembershipLevel() || current_user_can( 'manage_options' ) ) && ! is_admin() ){ | |
return; | |
} | |
if ( ! $q->is_main_query() ) return; | |
if ( ! $q->is_post_type_archive() ) return; | |
if ( ! is_admin() && is_shop() ) { | |
$q->set( 'tax_query', array(array( | |
'taxonomy' => 'product_cat', | |
'field' => 'slug', | |
'terms' => array( 'member-only-slug', 'another-product-slug' ), // Change it to the slug you want to hide | |
'operator' => 'NOT IN' | |
))); | |
} | |
remove_action( 'pre_get_posts', 'pmpro_remove_cat_from_shop' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment