Created
September 12, 2017 09:21
-
-
Save andrewlimaza/bf88f711297d5333eee80d6e8fc892be to your computer and use it in GitHub Desktop.
Stop non-members accessing WooCommerce products directly with Paid Memberships Pro
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 | |
/** | |
* This stops users from accessing restricted products directly with Paid Memberships Pro. | |
* Tested on WooCommerce 3.0 and up. | |
* You may be interested with the following gist for removing products from WooCommerce shop page - https://gist.github.com/andrewlimaza/6ee480694d38a99695503febe3cdabf1 | |
* Add the following function to your PMPro Customizations plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* Visit https://www.paidmembershipspro.com for any questions or assistance. | |
*/ | |
function pmpro_redirect_away_from_product_non_members(){ | |
global $post; | |
// Check to see if user is viewing single product or not. | |
if( !is_product() ) { | |
return; | |
} | |
// Bail if current user has an active membership or is administrator. | |
if( pmpro_hasMembershipLevel() || current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
// Get the product category for the product. | |
$product_cat = get_the_terms( $post->ID, 'product_cat' ); | |
$product_slug = $product_cat[0]->slug; | |
// If product has a category of 'member-only', redirect to home page. | |
if( $product_slug == 'member-only' ){ //change 'member-only' to product category that is restricted. | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} | |
add_action( 'template_redirect', 'pmpro_redirect_away_from_product_non_members' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment