Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/1b6d4ddbce768ee6727723511cc558db to your computer and use it in GitHub Desktop.
Save andrewlimaza/1b6d4ddbce768ee6727723511cc558db 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 );
@fbarnes
Copy link

fbarnes commented Feb 23, 2021

If $is_purchasable comes in as false, may want to just return false. The item may be marked not purchasable for some other reason, and having a pro membership generally wouldn't be a reason to override that.

@andrewlimaza
Copy link
Author

Thanks @fbarnes, I've updated the gist.

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