Duck hunt
Hướng dẫn show reviews (Star rating) dù không có đánh giá. - Zayquara1.Cf, Website giải trí,thông tin game
ZAYQUARA1.CF
Icon 1

Hướng dẫn show reviews (Star rating) dù không có đánh giá.

Hôm này, mình sẽ chia sẻ một đoạn code hiển thì star rating đối với các sản phẩm không có đánh giá ra ngoài trang chủ để mọi người có thể show sản phẩm được đẹp mắt hơn nhé.

Kết quả muốn đạt được

Hiển thị sao đối với các sản phẩm không có đánh giá

Copy đoạn function sau bỏ vào function.php của mình nhé.

function show_rating( $rating_html, $rating, $count ) {
   $rating_html = '<div class="star-rating">';
   $rating_html .= wc_get_star_rating_html( $rating, $count );
   $rating_html .= '</div>';

   return $rating_html;
}; 
add_filter( 'woocommerce_product_get_rating_html', 'show_rating', 100, 3 );

Như vậy là xong. Có gì cứ cmt các thắc mắc để bên mình hỗ trợ nhé.

Bonus: Hiển thị số lượt đánh giá ở shop page.

Để hiển thị số lượt đánh giá ở shop page như hình.

Hiển thị số lượt đánh giá

Bước đầu tiên bạn vào tạo một thư mục woocommerce trong theme của mình. (Đa phần chỉnh sửa template của woocommerce bạn đều phải tạo một thư mục riêng trong theme để tránh bị mất khi update theme/plugin)

Đường dẫn sẽ như sau: domain/YOUR-THEME/woocommerce/loop/rating.php

Bạn dán đoạn code sau vào file rating.php vừa tạo là xong.

<?php
/**
 * Loop Rating
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/rating.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     3.4.0
 */

if ( ! defined( 'ABSPATH' ) ) {
   exit;
}

global $product;

if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
   return;
}

$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average     = $product->get_average_rating();

if ( $rating_count >= 0 ) : ?>

           <?php echo wc_get_rating_html($average, $rating_count); ?>
       <?php if ( comments_open() ): ?><a href="<?php echo get_permalink() ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?>
   

<?php endif; ?>

Như vậy là xong, cảm ơn các bạn đã theo dõi

Back to posts
Comments:

Post a comment

TAGS - SEO
Tags: http://zayquara1.xtgem.com/blog
SEO : Bạn đến từ :