在Woocommerce的某些页面上添加产品ID的星级评分

问题描述 投票:2回答:1

有没有办法在自定义页面上的自定义页面上调用特定产品的星级评分?换句话说,我可以添加特定T恤的星级评分,以显示在我主页上的T恤图片下吗?

我想我需要在编辑器中添加某种PHP,然后用某种html调用它。我看到this answered thread,其中一个答案似乎帮助了人们,但是,它只给你必要的php,所以我不知道如何将它真正放在特定页面上的特定位置。我也不确定这个答案是否解决了在自定义页面上添加星标的可能性,而不仅仅是woocommerce页面。

该线程的另一个答案包含html。我尝试了它,我能够加载星星,除了它们与产品页面上的实际星级不相符。

我为类似的问题已经存在这一事实道歉,但是,它们似乎并没有直接解决我的需求,而且我也无法使它们发挥作用:/。

php html wordpress woocommerce rating
1个回答
2
投票

对于特定的产品ID,您可以使用一些专用的WC_Product方法:

// Get an instance of the WC_Product Object (from a product ID)
$product = wc_get_product( $product_id);

// The product rating count (number of reviews by rating )
$rating_count = $product->get_rating_counts(); // Multidimensional array

// The product average rating (or how many stars this product has)
$average_rating = $product->get_average_rating();

// Testing Output
echo '<p>Rating average: '.$average_rating.' stars</p>';

要显示产品“星星”的平均评分:

您可以使用专用函数wc_get_rating_html()get_average_rating() WC_Product方法。所以必要的代码将是:

// Get an instance of the WC_Product Object (from a product ID)
$product = wc_get_product( $product_id);

// The product average rating (or how many stars this product has)
$average_rating = $product->get_average_rating();

// The product stars average rating html formatted.
$average_rating_html = wc_get_rating_html($average_rating);

// Display stars average rating html
echo $average_rating_html;

经过测试和工作。

一个有趣的答案:Rating is showing numbers instead of stars


用于在任何地方显示产品星级的短代码 - 2个代码版本:

1)基于wc_get_rating_html()功能的最佳方式(适用于Woocommerce 3+):

add_shortcode( 'product_rating', 'display_the_product_rating' );
function display_the_product_rating( $atts ) {
    // Shortcode attributes
    $atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_rating' );

    if ( isset($atts['id']) && $atts['id'] > 0 ):

    // Get an instance of the WC_Product Object
    $product = wc_get_product( $atts['id'] );

    // The product average rating (or how many stars this product has)
    $average = $product->get_average_rating();

    endif;

    if ( isset($average) ) :

    return wc_get_rating_html($average);

    endif;
}

2)旧的方式(也有效):

add_shortcode( 'product_rating', 'display_the_product_rating' );
function display_the_product_rating( $atts ) {
    // Shortcode attributes
    $atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'product_rating' );

    if ( isset($atts['id']) && $atts['id'] > 0 ):

    // Get an instance of the WC_Product Object
    $product = wc_get_product( $atts['id'] );

    // The product average rating (or how many stars this product has)
    $average = $product->get_average_rating();

    // HERE the average width
    $average_width = $average * 16 . 'px';

    endif;

    if ( isset($average) ) :

    return '<div class="starwrapper" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
        <span class="star-rating" title="'.sprintf(__('Rated %s out of 5', 'woocommerce'), $average).'">
            <span style="width:'.$average_width.'">
                <span itemprop="ratingValue" class="rating">'.$average.'</span>
            </span>
        </span>
    </div><br clear="all">';

    endif;
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。


用法示例:

1)在WordPress页面或帖子文本编辑器中(其中37是产品ID):

[product_rating id='37']

2)在php代码中:

echo do_shortcode( "[product_rating id='37']" );

3)在html标签之间:

<?php echo do_shortcode( "[product_rating id='37']" ); ?>

你会得到类似的东西:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.