Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Forked from andrewlimaza/hide_prices_woo_pmpro.php
Last active February 26, 2020 03:31
Show Gist options
  • Select an option

  • Save ipokkel/c1eb1fa868e086ba31d5fb4272044d38 to your computer and use it in GitHub Desktop.

Select an option

Save ipokkel/c1eb1fa868e086ba31d5fb4272044d38 to your computer and use it in GitHub Desktop.
Hide prices + add to cart button for non members Paid Memberships Pro and WooCommerce
<?php
/**
* This recipe will only allow approved users of a certain levels to shop.
* Update line 25 with the membership level IDs to allow into the store.
*
* 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_shop() {
global $current_user;
if (
(
! is_user_logged_in() ||
(
is_user_logged_in()
&&
(
(
function_exists( 'pmpro_hasMembershipLevel' )
&& ! pmpro_hasMembershipLevel( array( 1, 2 ), $current_user->ID )
)
||
(
class_exists( 'PMPro_Approvals' )
&&
(
'pending' === PMPro_Approvals::getUserApprovalStatus() ||
'denied' === PMPro_Approvals::getUserApprovalStatus()
)
)
)
)
)
&&
(
function_exists( 'is_woocommerce' ) &&
( is_woocommerce() || is_cart() || is_checkout() )
)
) {
wp_redirect( pmpro_url( 'levels' ) );
exit;
}
}
add_action( 'template_redirect', 'my_pmpro_members_only_woocommerce_shop' );
/**
* Hide prices + add to cart from non members in Paid Memberships Pro.
* Add this custom code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* Visit www.paidmembershipspro.com for assistance.
*/
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() ) {
if ( class_exists( 'PMPro_Approvals' ) ) {
if ( 'pending' != PMPro_Approvals::getUserApprovalStatus() && 'denied' != PMPro_Approvals::getUserApprovalStatus() ) {
return;
}
} else {
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment