使用挂钩替换Woocommerce中的星级 [英] Replace star ratings in Woocommerce using hooks
问题描述
启用后,每个woocommerce产品均具有星级.我有一些自定义代码,我想全部替换当前的星级(包括#客户评论"部分.
when enabled, every woocommerce product has a star rating. I have some custom code that I would like to replace the current star rating in its entirety (Including the "# customer reviews" part.
当前,我正在使用的代码仅附加到星级,而不是完全替换它.以下是我看到的当前结果,以及已添加到functions.php中的代码.预先谢谢你.
Currently, the code I am using only appends to the star rating, rather than replace it fully. Below is the current result I am seeing, and the code I have added to functions.php. Thank you in advance.
代码:
add_filter( 'woocommerce_get_star_rating_html', 'replace_star_ratings' );
function replace_star_ratings( $variable ) {
echo "XXX";
return $variable;
}
推荐答案
要在不编辑模板的情况下完全替代产品上的星级评定,请使用以下命令:
To replace completely Stars rating on products without editing templates, use the following:
// For single product pages
add_action( 'woocommerce_single_product_summary', 'custom_rating_single_product_summary', 4 );
function custom_rating_single_product_summary() {
global $product;
if ( $product->get_rating_count() > 0 ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'woocommerce_single_product_summary', 'replace_product_rating', 9 );
}
}
// For Shop and archicves pages
add_action( 'woocommerce_after_shop_loop_item_title', 'custom_rating_after_shop_loop_item_title', 4 );
function custom_rating_after_shop_loop_item_title() {
global $product;
if ( $product->get_rating_count() > 0 ) {
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_rating', 5 );
add_action( 'woocommerce_after_shop_loop_item_title', 'replace_product_rating', 5 );
}
}
// Content function
function replace_product_rating() {
global $product;
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
echo '<div class="woocommerce-product-rating">'. __("XXX", "woocommerce") . '</div>';
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于使用挂钩替换Woocommerce中的星级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!