Ваш существующий код сложен, не завершен и не очень удобен. Вместо этого попробуйте следующее, которое будет работать для отдельных страниц продукта и страниц архивирования тоже (как страница магазина) .
Он обрабатывает любой вид продукта, включая переменные продукты и их вариации.
Для определенных категорий товаров он заменяет цену и отключает кнопку «Добавить в корзину» для связанных товаров.
код:
// Custom conditional function that check for specific product categories
function check_for_defined_product_categories( $product_id ) {
// HERE your Product Categories where the product price need to be hidden.
$targeted_terms = array( '27419','27421' ); // Can be term names, slugs or Ids
return has_term( $targeted_terms, 'product_cat', $product_id );
}
// Custom function that replace the price by a text
function product_price_replacement(){
return '<span style="color:red; font-size:12px;">' . sprintf( __( "Call our office %s for prices."), '<strong>516.695.3110</strong>' ) . '</span>';
}
// Replace price by a text (conditionally)
add_filter( 'woocommerce_get_price_html', 'filter_get_price_html_callback', 10, 2 );
function filter_get_price_html_callback( $price, $product ){
if( check_for_defined_product_categories( $product->get_id() ) ) {
$price = product_price_replacement();
}
return $price;
}
// Hide prices and availiability on product variations (conditionally)
add_filter( 'woocommerce_available_variation', 'filter_available_variation_callback', 10, 3 ); // for Variations
function filter_available_variation_callback( $args, $product, $variation ) {
if( check_for_defined_product_categories( $product->get_id() ) ) {
$args['price_html'] = '';
$args['availability_html'] = '';
}
return $args;
}
// Disable add to cart button (conditionally)
add_filter( 'woocommerce_variation_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
$product_id = $product->get_parent_id() > 0 ? $product->get_parent_id() : $product->get_id();
if( check_for_defined_product_categories( $product_id ) ) {
$purchasable = false;
}
return $purchasable;
}
Код помещается в файл functions.php вашей активной дочерней темы (или активной темы). Проверено и работает.