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 badges
Add a comment  | 

2 Answers 2

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