Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iWebbers/b266304d864974669080aaa741ba26d2 to your computer and use it in GitHub Desktop.
Save iWebbers/b266304d864974669080aaa741ba26d2 to your computer and use it in GitHub Desktop.
// Get WooCommerce random product ratings via shortcode: [iw_random_ratings]
function iw_random_ratings_shortcode( $atts ) {
extract( shortcode_atts( array(
'author__not_in' => '',
'number' => '5',
'author' => 'show',
'object' => 'show'
), $atts ) );
$args = array(
'author__not_in' => $author__not_in,
'number' => $number,
'status' => 'approve'
);
$comments = get_comments( $args );
shuffle($comments);
$content = '';
$content .= '<ul class="uk-list uk-list-divider">';
foreach( $comments as $comment ) {
$rating = get_comment_meta( $comment->comment_ID, 'rating', true );
if ($rating != 0)
{
$content .= '<li>';
$content .= '<div class="uk-grid-small uk-flex-middle" uk-grid>';
$content .= '<div class="uk-width-auto">';
$content .= '<div style="margin-top: -2px">';
foreach (array(1,2,3,4,5) as $val)
{
$size = '20';
$score = $rating - $val;
if ($score >= 0)
{
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1728 647q0 22-26 48l-363 354 86 500q1 7 1 20 0 21-10.5 35.5t-30.5 14.5q-19 0-40-12l-449-236-449 236q-22 12-40 12-21 0-31.5-14.5t-10.5-35.5q0-6 2-20l86-500-364-354q-25-27-25-48 0-37 56-46l502-73 225-455q19-41 49-41t49 41l225 455 502 73q56 9 56 46z" fill="#f7c725"></path></svg>';
} else if ($score > -1 && $score < 0) {
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1250 957l257-250-356-52-66-10-30-60-159-322v963l59 31 318 168-60-355-12-66zm452-262l-363 354 86 500q5 33-6 51.5t-34 18.5q-17 0-40-12l-449-236-449 236q-23 12-40 12-23 0-34-18.5t-6-51.5l86-500-364-354q-32-32-23-59.5t54-34.5l502-73 225-455q20-41 49-41 28 0 49 41l225 455 502 73q45 7 54 34.5t-24 59.5z" fill="#f7c725"></path></svg>';
} else {
$content .= '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $size . '" height="' . $size . '" viewBox="0 0 1792 1792"><path d="M1201 1004l306-297-422-62-189-382-189 382-422 62 306 297-73 421 378-199 377 199zm527-357q0 22-26 48l-363 354 86 500q1 7 1 20 0 50-41 50-19 0-40-12l-449-236-449 236q-22 12-40 12-21 0-31.5-14.5t-10.5-35.5q0-6 2-20l86-500-364-354q-25-27-25-48 0-37 56-46l502-73 225-455q19-41 49-41t49 41l225 455 502 73q56 9 56 46z" fill="#ccc"></path></svg>';
}
}
$content .= '</div>';
$content .= '</div>';
if ( $author == 'show' ) {
$content .= '<div class="uk-width-expand uk-text-truncate">';
$content .= $comment->comment_author;
$content .= '</div>';
}
$content .= '</div>';
if ( $object == 'show' ) {
$content .= '<p class="uk-margin-remove"><a href="' . esc_url( get_permalink( $comment->comment_post_ID ) ) . '"><span uk-icon="icon: triangle-right"></span> ' . get_the_title( $comment->comment_post_ID ) . '</a></p>';
}
$content .= '</li>';
}
}
$content .= '</ul>';
return $content;
}
add_shortcode( 'iw_random_ratings', 'iw_random_ratings_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment