Last active
September 16, 2021 07:44
-
-
Save andrewlimaza/1b6d4ddbce768ee6727723511cc558db to your computer and use it in GitHub Desktop.
Make certain WooCommerce products not-purchasable for non-members
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 | |
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.