If you know the id, you can simply check current product id in your woocommerce_get_price_html
action:
add_action('after_setup_theme','activate_filter') ;
function activate_filter() {
add_filter('woocommerce_get_price_html', 'show_price_logged');
}
function show_price_logged($price) {
global $product; // get current product
if(is_user_logged_in() || $product->id === 8) { // check product id
return $price;
} else {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Call for pricing</a>';
}
}
But if you need more flexibility you could check product custom field. For example, you could set is_free
custom field to true
or any other value of your choice on product edit page and check it’s value like this:
...
global $product;
$is_free_product = get_post_meta($product->id, 'is_free', true);
if(is_user_logged_in() || $is_free_product) {
return $price;
} else {
...