Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/3db8235582e8a361762ccbff8926912e to your computer and use it in GitHub Desktop.
Save dwanjuki/3db8235582e8a361762ccbff8926912e to your computer and use it in GitHub Desktop.
Hide WooCommerce Add to Cart buttons from users without the required membership level
<?php
/*
* Hide WooCommerce Add to Cart buttons from users without the required membership levels
*
* Specify Level IDs that are allowed to make purchases on line 16
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_members_only_woocommerce_add_to_cart() {
// if user has the specified PMPro membership level simply return.
if ( pmpro_hasMembershipLevel( array( 2 ) ) ) { // add level ID(s) here
return;
}
// hide add to cart button *ALL INSTANCES*
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
//hide the sales badge
add_filter('woocommerce_sale_flash', '__return_false');
}
add_action( 'wp', 'my_pmpro_members_only_woocommerce_add_to_cart' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment