Created
December 4, 2020 01:55
-
-
Save DeveloperWil/e1b637af9bed749b0fb5a49446ba2d0b to your computer and use it in GitHub Desktop.
WooCommerce: Display Product Reviews Shortcode
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
/** | |
* Use a shortcode to display product reviews. | |
* Format: [product_reviews id="123"] | |
* id = the product ID - you can get this from Products > All Products and hovering over the product title | |
* | |
* If there are no reviews for a product, nothing is returned to the browser. | |
* | |
* @author Wil Brown zeropointdevelopment.com | |
* @param $atts | |
* | |
* @return string | |
*/ | |
function zpd_product_reviews_shortcode( $atts ) { | |
if ( empty( $atts ) ) return ''; | |
if ( ! isset( $atts['id'] ) ) return ''; | |
$comments = get_comments( 'post_id=' . $atts['id'] ); | |
if ( ! $comments ) return ''; | |
$html = ''; | |
$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">'; | |
foreach ( $comments as $comment ) { | |
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ); | |
$html .= '<li class="review">'; | |
$html .= get_avatar( $comment, '60' ); | |
$html .= '<div class="comment-text">'; | |
if ( $rating ) $html .= wc_get_rating_html( $rating ); | |
$html .= '<p class="meta"><strong class="woocommerce-review__author">'; | |
$html .= get_comment_author( $comment ); | |
$html .= '</strong></p>'; | |
$html .= '<div class="description">'; | |
$html .= $comment->comment_content; | |
$html .= '</div></div>'; | |
$html .= '</li>'; | |
} | |
$html .= '</ol></div></div>'; | |
return $html; | |
} | |
add_shortcode( 'product_reviews', 'zpd_product_reviews_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment