WC_Comments::get_average_rating_for_product PHP Method

get_average_rating_for_product() public static method

Get product rating for a product. Please note this is not cached.
Since: 2.7.0
public static get_average_rating_for_product ( WC_Product &$product ) : float
$product WC_Product
return float
    public static function get_average_rating_for_product(&$product)
    {
        global $wpdb;
        $count = $product->get_rating_count();
        if ($count) {
            $ratings = $wpdb->get_var($wpdb->prepare("\n\t\t\t\tSELECT SUM(meta_value) FROM {$wpdb->commentmeta}\n\t\t\t\tLEFT JOIN {$wpdb->comments} ON {$wpdb->commentmeta}.comment_id = {$wpdb->comments}.comment_ID\n\t\t\t\tWHERE meta_key = 'rating'\n\t\t\t\tAND comment_post_ID = %d\n\t\t\t\tAND comment_approved = '1'\n\t\t\t\tAND meta_value > 0\n\t\t\t", $product->get_id()));
            $average = number_format($ratings / $count, 2, '.', '');
        } else {
            $average = 0;
        }
        $product->set_average_rating($average);
        $data_store = $product->get_data_store();
        $data_store->update_average_rating($product);
        return $average;
    }

Usage Example

 /**
  * Read product data. Can be overridden by child classes to load other props.
  *
  * @param WC_Product
  * @since 2.7.0
  */
 protected function read_product_data(&$product)
 {
     $id = $product->get_id();
     if ('' === ($review_count = get_post_meta($id, '_wc_review_count', true))) {
         WC_Comments::get_review_count_for_product($product);
     } else {
         $product->set_review_count($review_count);
     }
     if ('' === ($rating_counts = get_post_meta($id, '_wc_rating_count', true))) {
         WC_Comments::get_rating_counts_for_product($product);
     } else {
         $product->set_rating_counts($rating_counts);
     }
     if ('' === ($average_rating = get_post_meta($id, '_wc_average_rating', true))) {
         WC_Comments::get_average_rating_for_product($product);
     } else {
         $product->set_average_rating($average_rating);
     }
     $product->set_props(array('sku' => get_post_meta($id, '_sku', true), 'regular_price' => get_post_meta($id, '_regular_price', true), 'sale_price' => get_post_meta($id, '_sale_price', true), 'price' => get_post_meta($id, '_price', true), 'date_on_sale_from' => get_post_meta($id, '_sale_price_dates_from', true), 'date_on_sale_to' => get_post_meta($id, '_sale_price_dates_to', true), 'total_sales' => get_post_meta($id, 'total_sales', true), 'tax_status' => get_post_meta($id, '_tax_status', true), 'tax_class' => get_post_meta($id, '_tax_class', true), 'manage_stock' => get_post_meta($id, '_manage_stock', true), 'stock_quantity' => get_post_meta($id, '_stock', true), 'stock_status' => get_post_meta($id, '_stock_status', true), 'backorders' => get_post_meta($id, '_backorders', true), 'sold_individually' => get_post_meta($id, '_sold_individually', true), 'weight' => get_post_meta($id, '_weight', true), 'length' => get_post_meta($id, '_length', true), 'width' => get_post_meta($id, '_width', true), 'height' => get_post_meta($id, '_height', true), 'upsell_ids' => get_post_meta($id, '_upsell_ids', true), 'cross_sell_ids' => get_post_meta($id, '_crosssell_ids', true), 'purchase_note' => get_post_meta($id, '_purchase_note', true), 'default_attributes' => get_post_meta($id, '_default_attributes', true), 'category_ids' => $this->get_term_ids($product, 'product_cat'), 'tag_ids' => $this->get_term_ids($product, 'product_tag'), 'shipping_class_id' => current($this->get_term_ids($product, 'product_shipping_class')), 'virtual' => get_post_meta($id, '_virtual', true), 'downloadable' => get_post_meta($id, '_downloadable', true), 'gallery_image_ids' => array_filter(explode(',', get_post_meta($id, '_product_image_gallery', true))), 'download_limit' => get_post_meta($id, '_download_limit', true), 'download_expiry' => get_post_meta($id, '_download_expiry', true), 'image_id' => get_post_thumbnail_id($id)));
     // Gets extra data associated with the product.
     // Like button text or product URL for external products.
     foreach ($product->get_extra_data_keys() as $key) {
         $function = 'set_' . $key;
         if (is_callable(array($product, $function))) {
             $product->{$function}(get_post_meta($product->get_id(), '_' . $key, true));
         }
     }
 }
All Usage Examples Of WC_Comments::get_average_rating_for_product