Last active
December 16, 2022 04:04
-
-
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
This file contains 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
//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