I want to show to guest users price to products but only in some categories..this code disable product price and add to cart button for all products..to mention i have problem with hidding add to cart button, its not working (i use woodmart theme)
add_filter( 'woocommerce_get_price_html', 'hide_price_and_button', 9999, 2 );
function hide_price_and_button( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'bbloomer' ) . '</a></div>';
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 1000 );
}
return $price;
}
CodePudding user response:
You can use filter and set woocommerce_is_purchasable
to false.
add_filter( 'woocommerce_is_purchasable', '__return_false');
So you could end up with something like:
add_filter( 'woocommerce_get_price_html', 'hide_price', 9999, 2 );
function hide_price( $price, $product ) {
if ( ! is_user_logged_in() ) {
$price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Login to see prices', 'bbloomer' ) . '</a></div>';
}
return $price;
}
add_filter( 'woocommerce_is_purchasable', 'hide_button', 10, 2);
function hide_button( $is_purchasable, $object ) {
if ( ! is_user_logged_in() ) {
$is_purchasable = false;
}
return $is_purchasable;
}