I use the following code in the functions.php file in the child theme:
add_filter( 'woocommerce_variable_sale_price_html', 'update_price_html', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'update_price_html', 10, 2 );
add_filter( 'woocommerce_get_price_html','update_price_html', 9999, 2 );
function update_price_html( $html, $product ) {
if(!is_user_logged_in()) { // Si el usuario no está logueado
add_filter( 'woocommerce_is_purchasable', '__return_false');
$html = "Necesita registrarse para ver precios";
return $html;
} else {
return $html;
}
}
This code works perfectly fine to hide prices from unregistered users. But in the case of hiding the “Add to cart” button, it presents the problem that it does not hide it in the first product in the category list, although it does for all other products.
This problem occurs for any product category and / or subcategory, change the “Add to cart” button for the “Read more” button in all the products in the list except the first one.
2
Answers
To remove the add to cart button from the archive pages, you can use callback function
woocommerce_template_loop_add_to_cart
.Using this code in your function.php file can hide the add to cart button for unregistered users and direct them to login to view the prices to buy.
It also hides the add to cart button in shop page , product category page and product detail page. Enjoy!