Forked from andrewlimaza/hide_prices_woo_pmpro.php
Last active
December 17, 2020 05:02
-
-
Save MaryOJob/7d386c72261ae816627a65d111d7eed7 to your computer and use it in GitHub Desktop.
Hide prices + add to cart button for non members Paid Memberships Pro and WooCommerce & Redirect Non Members away from Cart, Checkout & Single Products Page
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 / DO NOT COPY THIS LINE PLEASE!! | |
/** | |
* Hide prices + add to cart from non members in Paid Memberships Pro. | |
* Redirect non members from cart, checkout and single products page. | |
* 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() ){ | |
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() || is_product() ){ | |
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