Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save travislima/fe3b652ed1e58175c44e58d1dc37a90c to your computer and use it in GitHub Desktop.
Save travislima/fe3b652ed1e58175c44e58d1dc37a90c to your computer and use it in GitHub Desktop.
Make certain WooCommerce products not-purchasable for non-members
<?php
/**
* Stop non-members from purchasing products if they do not have an active Paid Memberships Pro Level.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function stop_non_pmpro_members_from_buying_woo( $is_purchasable, $product ) {
// Check if the user has an active membership level.
if( pmpro_hasMembershipLevel() ) {
return $is_purchasable;
}
// Adjust the restricted categories that require membership level to purchase. Use category-slug.
$restricted_category_slugs = array(
'category-1',
'category-2',
'category-3'
);
if( has_term( $restricted_category_slugs, 'product_cat', $product->id ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'stop_non_pmpro_members_from_buying_woo', 10, 2 );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Require Membership to Purchase Specific Categories of Products in WooCommerce" at Paid Memberships Pro here: https://www.paidmembershipspro.com/require-membership-to-purchase-specific-categories-of-products-in-woocommerce/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment