This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Products attributes in the resultbox

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Products attributes in the resultbox Reply To: Products attributes in the resultbox

#26054
Ernest MarcinkoErnest Marcinko
Keymaster

You are welcome 🙂

Well, there was a custom-coded solution that worked a long time ago. I am not sure if that is still the case, but it is worth a try. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

add_filter( 'asp_results', 'asp_add_woo_ratings', 1, 1 );
function asp_my_print_stars( $id ){
    global $wpdb;
    $count = $wpdb->get_var("
        SELECT COUNT(meta_value) FROM $wpdb->commentmeta
        LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
        WHERE meta_key = 'rating'
        AND comment_post_ID = $id
        AND comment_approved = '1'
        AND meta_value > 0
    ");
    
    $rating = $wpdb->get_var("
        SELECT SUM(meta_value) FROM $wpdb->commentmeta
        LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
        WHERE meta_key = 'rating'
        AND comment_post_ID = $id
        AND comment_approved = '1'
    ");
    
    $out = '';
    if ( $count > 0 ) {
      $average = number_format($rating / $count, 2);
      ob_start();
      ?>
      <div class="woocommerce">
      <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating" class="star-rating" title="<?php sprintf(__('Rated %s out of 5', 'woocommerce'), $average); ?>">
      		<span style="width:<?php echo ($average*20); ?>%"><strong itemprop="ratingValue">&nbsp;</strong></span>
      </div>
      </div>
      <?php
      $out = ob_get_clean();
    }                                    

    return $out;
}
 
function asp_add_woo_ratings( $results ) {
  foreach ($results as $k=>&$v) {
    if ( 
      $v->content_type == 'pagepost' &&
      in_array($v->post_type, array('product', 'product_variation'))
     ) {
      $v->content .= asp_my_print_stars($v->id);
    }
  }
  unset($v);
  return $results;
}