如何在 woocommerce 购物车中的产品标题下方显示评论?
How to display reviews on an under product title in cart of woocommerce?
我正在尝试添加产品的评论数量和星级评论。我想实现这样的目标。
我用来实现它的片段(不完全但相似)是这个
add_filter('woocommerce_cart_item_name', 'custom_item_display_on_cart', 10, 3);
function custom_item_display_on_cart($name, $cart_item, $item_key){
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$thumbnail = $_product->get_image();
$price = $_product->get_price_html();
$rating = woocommerce_review_display_rating();
$product_variation = '';
if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0 ){
if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){
foreach ($cart_item['variation'] as $key => $value) {
$product_variation .= ''.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value);
}
}
}
echo '<div class="product-item-wrapper"> '. '<div class="product-thumbnail-wrapper">' . $thumbnail. '</div>' . '<div class="item-wrapper">' . '<div class="product-name">' . $name . '</div>' . '<div class="product-variation">' . $product_variation . '</div>' .'<div class="product-price-item">' . $price . '</div>'. '</div>' . ' </div>' .$rating;
这是我使用的代码片段的结果
我快要实现了,但我面临的唯一问题是开始评级评论和评论数量。我尝试使用 woocommerce_review_display_rating();
但它没有显示任何内容。
提前致谢!
您可以使用 woocommerce_after_cart_item_name
操作挂钩在标题下方显示评论。代码将进入您的活动主题 functions.php 文件。
add_filter('woocommerce_after_cart_item_name', 'woocommerce_after_cart_item_name', 10, 2);
function woocommerce_after_cart_item_name( $cart_item, $cart_item_key ){
$product = wc_get_product( $cart_item['product_id'] );
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if ( $rating_count > 0 ) : ?>
<div class="woocommerce-product-rating">
<?php echo wc_get_rating_html( $average, $rating_count ); // WPCS: XSS ok. ?>
<div class="count"><?php echo esc_html( $review_count ); ?> Reviews</div>
</div>
<?php endif;
}
已测试并有效
我正在尝试添加产品的评论数量和星级评论。我想实现这样的目标。
我用来实现它的片段(不完全但相似)是这个
add_filter('woocommerce_cart_item_name', 'custom_item_display_on_cart', 10, 3);
function custom_item_display_on_cart($name, $cart_item, $item_key){
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$thumbnail = $_product->get_image();
$price = $_product->get_price_html();
$rating = woocommerce_review_display_rating();
$product_variation = '';
if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0 ){
if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){
foreach ($cart_item['variation'] as $key => $value) {
$product_variation .= ''.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value);
}
}
}
echo '<div class="product-item-wrapper"> '. '<div class="product-thumbnail-wrapper">' . $thumbnail. '</div>' . '<div class="item-wrapper">' . '<div class="product-name">' . $name . '</div>' . '<div class="product-variation">' . $product_variation . '</div>' .'<div class="product-price-item">' . $price . '</div>'. '</div>' . ' </div>' .$rating;
这是我使用的代码片段的结果
我快要实现了,但我面临的唯一问题是开始评级评论和评论数量。我尝试使用 woocommerce_review_display_rating();
但它没有显示任何内容。
提前致谢!
您可以使用 woocommerce_after_cart_item_name
操作挂钩在标题下方显示评论。代码将进入您的活动主题 functions.php 文件。
add_filter('woocommerce_after_cart_item_name', 'woocommerce_after_cart_item_name', 10, 2);
function woocommerce_after_cart_item_name( $cart_item, $cart_item_key ){
$product = wc_get_product( $cart_item['product_id'] );
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if ( $rating_count > 0 ) : ?>
<div class="woocommerce-product-rating">
<?php echo wc_get_rating_html( $average, $rating_count ); // WPCS: XSS ok. ?>
<div class="count"><?php echo esc_html( $review_count ); ?> Reviews</div>
</div>
<?php endif;
}
已测试并有效