删除 WooCommerce 评论选项卡上的 (0)
Removing the (0) on the WooCommerce Reviews Tab
当没有评论时,我已成功删除“评论”选项卡标题上的 (0)。在市场营销中——最好不要显示产品有 0 条评论。这是我放在 child 主题的 functions.php 文件中的代码,该文件位于 WooCommerce 插件文件 wc-template-function.php:
if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {
/**
* Add default product tabs to product pages.
*
* @param array $tabs
* @return array
*/
function woocommerce_default_product_tabs( $tabs = array() ) {
global $product, $post;
// Description tab - shows product content
if ( $post->post_content ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab'
);
}
// Additional information tab - shows attributes
if ( $product && ( $product->has_attributes() || ( $product->enable_dimensions_display() && ( $product->has_dimensions() || $product->has_weight() ) ) ) ) {
$tabs['additional_information'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 20,
'callback' => 'woocommerce_product_additional_information_tab'
);
}
// Reviews tab - shows comments
if ( comments_open() ) {
$check_product_review_count = $product->get_review_count();
if ( $check_product_review_count == 0 ) {
$tabs['reviews'] = array(
'title' => sprintf( __( 'Reviews', 'woocommerce' ) ),
'priority' => 30,
'callback' => 'comments_template'
);
}
else {
$tabs['reviews'] = array(
'title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ), $product->get_review_count() ),
'priority' => 30,
'callback' => 'comments_template'
);
}
}
return $tabs;
}
}
我的问题是 - 这是在不更改 woocommerce 核心文件的情况下修改它的最有效方法吗?函数 "woocommerce_default_product_tabs" 是一个可插入函数,但似乎我可以以某种方式使用过滤器而不是将整个函数复制到我的 child 主题中并从那里进行编辑。我只需要得到这行代码:
title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ),
并添加一个 if 语句来检查是否没有注释来更改上面的行,如上面的行:
title' => sprintf( __( 'Reviews', 'woocommerce' ),
这很容易。您可以更改任何选项卡的标题:
add_filter( 'woocommerce_product_tabs', 'wp_woo_rename_reviews_tab', 98);
function wp_woo_rename_reviews_tab($tabs) {
global $product;
$check_product_review_count = $product->get_review_count();
if ( $check_product_review_count == 0 ) {
$tabs['reviews']['title'] = 'Reviews';
} else {
$tabs['reviews']['title'] = 'Reviews('.$check_product_review_count.')';
}
return $tabs;
}
当没有评论时,我已成功删除“评论”选项卡标题上的 (0)。在市场营销中——最好不要显示产品有 0 条评论。这是我放在 child 主题的 functions.php 文件中的代码,该文件位于 WooCommerce 插件文件 wc-template-function.php:
if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {
/**
* Add default product tabs to product pages.
*
* @param array $tabs
* @return array
*/
function woocommerce_default_product_tabs( $tabs = array() ) {
global $product, $post;
// Description tab - shows product content
if ( $post->post_content ) {
$tabs['description'] = array(
'title' => __( 'Description', 'woocommerce' ),
'priority' => 10,
'callback' => 'woocommerce_product_description_tab'
);
}
// Additional information tab - shows attributes
if ( $product && ( $product->has_attributes() || ( $product->enable_dimensions_display() && ( $product->has_dimensions() || $product->has_weight() ) ) ) ) {
$tabs['additional_information'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 20,
'callback' => 'woocommerce_product_additional_information_tab'
);
}
// Reviews tab - shows comments
if ( comments_open() ) {
$check_product_review_count = $product->get_review_count();
if ( $check_product_review_count == 0 ) {
$tabs['reviews'] = array(
'title' => sprintf( __( 'Reviews', 'woocommerce' ) ),
'priority' => 30,
'callback' => 'comments_template'
);
}
else {
$tabs['reviews'] = array(
'title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ), $product->get_review_count() ),
'priority' => 30,
'callback' => 'comments_template'
);
}
}
return $tabs;
}
}
我的问题是 - 这是在不更改 woocommerce 核心文件的情况下修改它的最有效方法吗?函数 "woocommerce_default_product_tabs" 是一个可插入函数,但似乎我可以以某种方式使用过滤器而不是将整个函数复制到我的 child 主题中并从那里进行编辑。我只需要得到这行代码:
title' => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ),
并添加一个 if 语句来检查是否没有注释来更改上面的行,如上面的行:
title' => sprintf( __( 'Reviews', 'woocommerce' ),
这很容易。您可以更改任何选项卡的标题:
add_filter( 'woocommerce_product_tabs', 'wp_woo_rename_reviews_tab', 98);
function wp_woo_rename_reviews_tab($tabs) {
global $product;
$check_product_review_count = $product->get_review_count();
if ( $check_product_review_count == 0 ) {
$tabs['reviews']['title'] = 'Reviews';
} else {
$tabs['reviews']['title'] = 'Reviews('.$check_product_review_count.')';
}
return $tabs;
}