且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改单个产品页面上的评论评级位置

更新时间:2023-02-11 12:16:25

如果您打开 模板 woocommerce 文件 content-single-product.php,你会在里面看到:

If you open the template woocommerce file content-single-product.php, you will see that inside:

    /**
     * woocommerce_single_product_summary hook.
     *
     * @hooked woocommerce_template_single_title - 5
     * @hooked woocommerce_template_single_rating - 10
     * @hooked woocommerce_template_single_price - 10
     * @hooked woocommerce_template_single_excerpt - 20
     * @hooked woocommerce_template_single_add_to_cart - 30
     * @hooked woocommerce_template_single_meta - 40
     * @hooked woocommerce_template_single_sharing - 50
     */
    do_action( 'woocommerce_single_product_summary' );

因此,这将向您展示在 woocommerce_single_product_summary 操作挂钩中挂钩的不同模板及其优先级(末尾的小数字).

So this show you the different templates hooked in woocommerce_single_product_summary action hook with their priorities (the little number at the end).

这样你就可以重用我的代码 并用这个替换它:

So you can reuse my code and replace it with this one:

function customizing_simple_products(){
    global $product;

    if($product->is_type('simple')) // Only for simple products (thanks to helgathevicking)
    {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10); 
        add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 25);
        add_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 26); 
    }
}
add_action('woocommerce_before_single_product', 'customizing_simple_products');

并且您需要在活动主题的 style.css 中添加一些 CSS 规则(***使用子主题)将产品价格与此评级并列.为此,您必须针对正确的选择器,有时您必须将 !important 添加到该规则中,以使其有效.如果需要,您还可以在我的回答功能中操作优先级数字...

And you will need to add some CSS rules in the style.css of your active theme (is better use a child theme) to get the product price side by side with this ratings. For this you have to target the correct selectors and sometimes you will be obliged to add !important to that rules, to make them effective. You can also manipulate the priority numbers if needed in my answer function…

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

代码已经过测试并且可以正常工作.

The code is tested and works.