如何在主题的functions.php文件中使用Woocommerce模板代码

时间:2018-09-25 19:22:21

标签: php wordpress woocommerce product shortcode

我想显示每个woocommerce产品的评论计数,因此我遵循了link及其对所有产品的使用。

但是我还借助另一个插件添加了最近的评论部分。没有显示计数数字的地方。

原因是该插件未找到ratting.php代码,因此我想知道如何将rating.php代码用作主题functions.php文件。换句话说,我如何将这些代码转换为函数,以便可以将其转换为functions.php文件

这是rating.php的代码

<?php
/**
 * Loop Rating
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/loop/rating.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     3.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

global $product;

if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' ) {
    return;
}

$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average      = $product->get_average_rating();

if ( $rating_count >= 0 ) : ?>

            <?php echo wc_get_rating_html($average, $rating_count); ?>
        <?php if ( comments_open() ): ?><a href="<?php echo get_permalink() ?>#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?>


<?php endif; ?>

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用 id 自变量(产品ID) 将代码作为简码嵌入函数中:

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

    if ( get_option( 'woocommerce_enable_review_rating' ) === 'no' )
        return; // Exit

    global $product, $post;

    if( ! is_a( $product, 'WC_product') ){
        $product_id   = $atts['id'] > 0 ? $atts['id'] : get_the_id();
        $product      = wc_get_product( $product_id );
        if( ! is_a( $product, 'WC_product') ) 
            return; // Exit
    }

    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();
    $average      = $product->get_average_rating();
    $output       = '';

    if ( $rating_count >= 0 ) {
        $output .= wc_get_rating_html($average, $rating_count);
        if ( comments_open() ) {
            $fcount = $printf( _n( '%s',$review_count,'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' );
            $output .= '<a href="'.get_permalink().'#reviews" class="woocommerce-review-link" rel="nofollow">('.$fcount.')</a>';
        }
    }
    return $output;
}

代码进入活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。


用法示例:

1)在帖子或页面的WordPress文本编辑器中,定义 id 自变量(产品ID)

[product_rating id='37']

2)在一些带有动态$product_id 变量(产品ID) 的php代码中:

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

3)在WP_QueryWC_Product_Query的乘积循环内:

echo do_shortcode( "[product_rating]" );