Forked from andrewlimaza/stop-non-pmpro-members-from-buying-woocommerce.php
Last active
April 12, 2021 20:23
-
-
Save travislima/fe3b652ed1e58175c44e58d1dc37a90c to your computer and use it in GitHub Desktop.
Make certain WooCommerce products not-purchasable for non-members
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 | |
/** | |
* 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
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/