Disabling Add to Cart Button for Specific WooCommerce Products
-
I want to prevent logged out users from purchasing a particular product on WooCommerce
The code below prevents all products from being purchased.
My question is how can I enable it for just one product or product category?
// Replacing add-to-cart button in shop pages and archives pages (for non logged in users) add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 ); function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) { if ( ! is_user_logged_in() ) { $link = get_permalink($product_id); $button_text = __( "View product", "woocommerce" ); $html = '<a href="'.$link.'" class="button alt add_to_cart_button">'.$button_text.'</a>'; } return $html; } // Avoid add to cart for non logged user (or not registered) add_filter( 'woocommerce_add_to_cart_validation', 'logged_in_customers_validation', 10, 3 ); function logged_in_customers_validation( $passed, $product_id, $quantity) { if( ! is_user_logged_in() ) { $passed = false; // Displaying a custom message $message = __("You need to be logged in to be able to fund your EZYCoin wallet…", "woocommerce"); $button_link = get_permalink( get_option('woocommerce_myaccount_page_id') ); $button_text = __("Sign in", "woocommerce"); $message .= ' <a href="'.$button_link.'" class="login-register button" style="float:right;">'.$button_text.'</a>'; wc_add_notice( $message, 'error' ); } return $passed; }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Disabling Add to Cart Button for Specific WooCommerce Products’ is closed to new replies.