Skip to content

Instantly share code, notes, and snippets.

@jameshwartlopez
Last active December 16, 2022 04:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshwartlopez/3fe1b860fa77d814fac908bace3da83c to your computer and use it in GitHub Desktop.
Save jameshwartlopez/3fe1b860fa77d814fac908bace3da83c to your computer and use it in GitHub Desktop.
Remove/Hide add to cart button in Woocommerce if the user is not logged in
//Option One (if you decided to use Option Two then remove this function and its action hook)
function remove_add_cart_button(){
$isLoggedIn = is_user_logged_in();
if(false == $isLoggedIn){
//if the user is not login then remove add to cart button by removing the action hook
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
add_action('wp','remove_add_cart_button');
//Option Two (If you decided to use Option One then don't add the following code)
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$isLoggedIn = is_user_logged_in();
if(true == $isLoggedIn){
//Make product purchasable to logged in user
return true;
}
//Make product not purchasable to unlogged in user
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment