Ottenere la valutazione del prodotto tramite ID prodotto
11 set 2017, 08:55:53
Visualizzazioni: 23.1K
Voti: 6
Come ottenere la valutazione di un prodotto tramite product_id senza utilizzare un ciclo?
Ho un product_id e voglio ottenere la valutazione del prodotto, come posso farlo e è fattibile? Grazie
// Metodo 1: Utilizzando get_post_meta
$rating = get_post_meta($product_id, '_wc_average_rating', true);
// Metodo 2: Utilizzando la classe WC_Product
$product = wc_get_product($product_id);
$rating = $product->get_average_rating();
// Metodo 3: Utilizzando la funzione get_rating_html()
$product = wc_get_product($product_id);
$rating_html = $product->get_rating_html(); // Restituisce l'HTML della valutazione

ttn_
317
Tutte le risposte alla domanda
3
0
Dato un ID prodotto puoi ottenere la valutazione media in questo modo:
$product = wc_get_product( $product_id );
$rating = $product->get_average_rating();
Questo restituirà il numero grezzo (4.00, 3.50 etc.).
Per visualizzare l'HTML della valutazione per un prodotto specifico puoi usare questo codice:
$product = wc_get_product( $product_id );
$rating = $product->get_average_rating();
$count = $product->get_rating_count();
echo wc_get_rating_html( $rating, $count );
Oppure, se sei all'interno del loop puoi usare questa funzione per ottenere l'HTML del prodotto corrente:
woocommerce_template_loop_rating()

Jacob Peattie
43.9K
11 set 2017 09:23:47
0
Questo mi è stato molto utile, crea la funzione get_star_rating() e restituisci il tuo html.
NOTA: Se è all'interno di un loop
function get_star_rating() {
global $woocommerce, $product;
$average = $product->get_average_rating();
$review_count = $product->get_review_count();
return '<div class="star-rating">
<span style="width:'.( ( $average / 5 ) * 100 ) . '%" title="'.
$average.'">
<strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'su 5', 'woocommerce' ).
'</span>
</div>'.'
<a href="#reviews" class="woocommerce-review-link" rel="nofollow">( ' . $review_count .' )</a>';
}

Samael Pereira SImões
11
18 nov 2018 00:07:08
0
Puoi recuperare i prodotti con la valutazione più alta nel loop
$args_top_rating1 = array(
'post_type' => 'product',
'meta_key' => '_wc_average_rating',
'orderby' => 'meta_value',
'posts_per_page' => 8,
'status'=>'publish',
'catalog_visibility'=>'visible',
'stock_status'=>'instock'
);
$top_rating = new WP_Query( $args_top_rating1 );
while ( $top_rating->have_posts() ) : $top_rating->the_post(); global $product;
$urltop_rating = get_permalink($top_rating->post->ID) ;
$rating_count = $product->get_rating_count();
$average_rating = $product->get_average_rating();
echo wc_get_rating_html( $average_rating, $rating_count);
endwhile;

Tarani Joshi
11
9 set 2019 08:31:16
Domande correlate