Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Forked from andrewlimaza/hide_prices_woo_pmpro.php
Last active February 7, 2022 14:39
Show Gist options
  • Save kimwhite/ca96b3e4035fbeded549cf9be263c654 to your computer and use it in GitHub Desktop.
Save kimwhite/ca96b3e4035fbeded549cf9be263c654 to your computer and use it in GitHub Desktop.
Hide prices + add to cart button for non members and some levels Paid Memberships Pro and WooCommerce
/**
* Hide prices + add to cart from non members and some levels Paid Memberships Pro.
* Modify line 29 - Add Level ID of user who CAN purchase from your store
* Add this custom code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
*/
function remove_my_woo_prices( $price, $product ) {
global $pmprowoo_product_levels;
//no product levels or PMProWC not active
if( empty( $pmprowoo_product_levels ) ){
return '';
}
//check if the product is a membership level
$product_ids = array_keys( $pmprowoo_product_levels );
if( !in_array( $product->get_id(), $product_ids ) ) {
return '';
}
//must be a level product
return $price;
}
function hide_prices_for_non_pmpro_members(){
//if user has a PMPro membership level simply return.
if ( pmpro_hasMembershipLevel( array( 1, 4, 5 ) ) ){ // add your level(s) ID here
return;
}
//set price of all products to NULL
add_filter( 'woocommerce_variable_sale_price_html', 'remove_my_woo_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_my_woo_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'remove_my_woo_prices', 10, 2 );
//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', 'hide_prices_for_non_pmpro_members' );
function move_users_away_from_woo(){
//if user has a PMPro membership level simply return.
if( pmpro_hasMembershipLevel() ){
return;
}
//if the user ends up on the checkout or cart page, redirect to the home page.
if( is_checkout() || is_cart() ){
wp_redirect( home_url() ); //change this to another URL if needed.
exit;
}
}
add_action( 'template_redirect', 'move_users_away_from_woo' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment