Hide product prices and add-to-cart buttons but not variations for unregistered users in WooCommerce

Viewed 1538

In my WooCommerce shop I want to hide the prices until the customer has logged in. I've got the following code working that does exactly that:

add_action('init','hide_price');
function hide_price(){
    if(!is_user_logged_in()){
        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',30);
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_price',10);
        remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
        add_action('woocommerce_single_product_summary','print_login_to_see',31);
        add_action('woocommerce_after_shop_loop_item','print_login_to_see',11);
    }
}

function print_login_to_see(){
    echo '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">' . __('Login to see prices','theme_name') . '</a>';
}

However this also removes the variation drop down and I would like to keep that.

Is there any direct way to keep the variation drop down menu but still hide the prices until the customer has logged in?

Thanks

1 Answers
Related