admin管理员组文章数量:1406951
I haven't been able to find this on google and I thought this was a common requirement.
Is there a way to add a 5 stars (or whatever rating) to my products with a single plugin or PHP code? I need to show stars in every catalog so I can test my design.
I haven't been able to find this on google and I thought this was a common requirement.
Is there a way to add a 5 stars (or whatever rating) to my products with a single plugin or PHP code? I need to show stars in every catalog so I can test my design.
Share Improve this question asked Aug 24, 2017 at 5:46 Juan BonnettJuan Bonnett 1211 silver badge5 bronze badges2 Answers
Reset to default 5@Nikola's answer gives you what you want to need without saving anything to the database. If you need to save the values to the database and directly execute SQL, you can do the following.
Basically WooCommerce stores product reviews data as a postmeta under meta keys
- _wc_rating_count
- _wc_average_rating
- _wc_review_count
UPDATE fs_postmeta SET meta_value = 5
WHERE meta_key = '_wc_average_rating' AND post_id = 564;
UPDATE fs_postmeta SET meta_value = 105
WHERE meta_key = '_wc_review_count' AND post_id = 564;
or
<?php
/*
Plugin Name: BK
Version: 0.0.1
*/
function bk_get_all_products( ) {
$ps = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1'
) );
$arr = array();
while($ps->have_posts()){
$ps->the_post();
$arr[] = get_the_ID();
}
return $arr;
}
add_action('init','bk');
function bk(){
$products = bk_get_all_products();
// $re = array(
// "1" => 1,
// "2" => 2,
// "3" => 3,
// "4" => 5,
// "5" => 6,
// );
foreach ($products as $key => $value) {
update_post_meta( $value, '_wc_average_rating', 1 );
update_post_meta( $value, '_wc_review_count', 15 );
// update_post_meta( $value, '_wc_rating_count', $re );
}
}
Remember with this we are not changing the reviews list that appears under product tabs.
Since you need it just for visual purposes, it's better if you don't save the rating value to the database, but instead to just modify the value during runtime:
/**
* Modifies WooCommerce product rating to force a defined value
*
* @return string Modified rating html
*/
function wpse_get_modified_rating_html(){
/** @var float Rating being shown */
$rating = 5;
/** @var int Total number of ratings */
$count = 1;
$html = '<div class="star-rating">';
$html .= wc_get_star_rating_html( $rating, $count );
$html .= '</div>';
return $html;
}
add_filter( 'woocommerce_product_get_rating_html', 'wc_get_rating_html' );
You can add that code to your theme's functions.php
or you could create a new plugin for that purpose.
Remember to remove the code on production server, since it will force all ratings to show 5 stars!
本文标签: WoocommerceHow to add 5 stars to all products to test design
版权声明:本文标题:Woocommerce - How to add 5 stars to all products to test design 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744954773a2634289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论