Created
January 8, 2025 14:24
-
-
Save prantomollick/aee03eb74fc31f6cc80f74dde4aae111 to your computer and use it in GitHub Desktop.
Customer reviews for woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function dynamic_product_reviews_section() { | |
global $product; | |
// Get product data | |
$average_rating = $product->get_average_rating(); | |
$review_count = $product->get_review_count(); | |
$ratings_count = $product->get_rating_counts(); | |
if ($review_count > 0): // Only show if there are reviews | |
?> | |
<div class="tp-product-details-review-number d-inline-block mb-50"> | |
<h3 class="tp-product-details-review-number-title"> | |
<!-- | |
Enter Any Title Here | |
or | |
Daynamic count the review and with title | |
--> | |
<?php | |
$count = $product->get_review_count(); | |
if ( $count && wc_review_ratings_enabled() ) { | |
/* translators: 1: reviews count 2: product name */ | |
$reviews_title = sprintf( esc_html( _n( '%1$s Customer review for %2$s', '%1$s Customer reviews for %2$s', $count, 'woocommerce' ) ), esc_html( $count ), '<span>' . get_the_title() . '</span>' ); | |
echo apply_filters( 'woocommerce_reviews_title', $reviews_title, $count, $product ); // WPCS: XSS ok. | |
} else { | |
esc_html_e( 'Reviews', 'woocommerce' ); | |
} | |
?> | |
</h3> | |
<div class="tp-product-details-review-summery d-flex align-items-center"> | |
<div class="tp-product-details-review-summery-value"> | |
<span><?php echo esc_html($average_rating); ?></span> | |
</div> | |
<div class="tp-product-details-review-summery-rating d-flex align-items-center"> | |
<?php echo wc_get_rating_html($average_rating); ?> | |
<p>(<?php echo esc_html($review_count); ?> Reviews)</p> | |
</div> | |
</div> | |
<div class="tp-product-details-review-rating-list"> | |
<?php | |
// Define star levels (5 to 1 stars) | |
for ($star = 5; $star >= 1; $star--) { | |
$count = isset($ratings_count[$star]) ? $ratings_count[$star] : 0; | |
$percent = ($review_count > 0) ? ($count / $review_count) * 100 : 0; | |
?> | |
<!-- single item --> | |
<div class="tp-product-details-review-rating-item d-flex align-items-center"> | |
<span><?php echo esc_html($star); ?> Star</span> | |
<div class="tp-product-details-review-rating-bar"> | |
<span class="tp-product-details-review-rating-bar-inner" style="width: <?php echo esc_attr($percent); ?>%;"></span> | |
</div> | |
<div class="tp-product-details-review-rating-percent"> | |
<span><?php echo esc_html(round($percent, 1)); ?>%</span> | |
</div> | |
</div> | |
<!-- end single item --> | |
<?php } ?> | |
</div> | |
</div> | |
<?php | |
endif; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment