Created
October 4, 2023 15:08
-
-
Save titomus/3cfa6affa2618b4e51000c91c0c81a05 to your computer and use it in GitHub Desktop.
Display Woocommerce Reviews outside product page
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
function display_woocommerce_reviews($atts) { | |
// Extraire les attributs du shortcode | |
extract(shortcode_atts(array( | |
'product_id' => '0', | |
), $atts)); | |
// Vérifier si l'ID produit est valide | |
if(!$product_id || $product_id === '0') { | |
return "Veuillez spécifier un ID produit valide."; | |
} | |
// Récupérer les avis liés à ce produit | |
$comments = get_comments( 'post_id=' . $product_id ); | |
// Construire le HTML pour les afficher | |
$output = "<div class='woocommerce-reviews'>"; | |
if(empty($comments)) { | |
$output .= "Il n'y a pas encore d'avis pour ce produit."; | |
} else { | |
foreach($comments as $comment) { | |
$rating = intval(get_comment_meta($comment->comment_ID, 'rating', true)); | |
$avatar = get_avatar($comment->comment_author_email,48); | |
$output .= "<div class='single-review'>"; | |
$output .= "<div class='review-avatar' style='float: left; margin-right: 15px;'>" . $avatar . "</div>"; | |
$output .= "<div class='review-body'>"; | |
$output .= "<h3>Avis laissé par " . $comment->comment_author . "</h3>"; | |
$output .= "<div class='star-rating'>"; | |
for($i = 1; $i <= 5; $i++) { | |
$output .= ($i <= $rating) ? "★" : "☆"; | |
} | |
$output .= "</div>"; // Fin de star-rating | |
$output .= "<p>" . $comment->comment_content . "</p>"; | |
$output .= "</div>"; // Fin de review-body | |
$output .= "</div><hr/>"; // Fin de single-review | |
} | |
} | |
$output .= "</div>"; // Fin de woocommerce-reviews | |
return $output; | |
} | |
add_shortcode('display_reviews', 'display_woocommerce_reviews'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment