如何在函数内定位 WooCommerce 产品循环

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

循环/price.php

我尝试了这个并且有效,但不确定这是不是好方法

<?php if ( $price_html = $product->get_price_html() ) : ?>
    <?php add_filter( 'woocommerce_format_sale_price', 'test_loop_price', 10, 3 );
    ?>
    <span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
php templates woocommerce product hook-woocommerce
1个回答
0
投票

是的,这是一个坏主意,因为这不是正确的方法......显然,您正在使用名为

test_loop_price()
的自定义函数,并且您没有在问题中提供该函数的代码。

首先从

loop/price.php
模板文件中删除您的自定义代码。

然后,您需要在该函数内定位 WooCommerce 产品循环,例如:


add_filter( 'woocommerce_format_sale_price', 'test_loop_price', 10, 3 );
function test_loop_price( $price, $regular_price, $sale_price ) {
    global $woocommerce_loop, $product;

    if ( ( is_product() && isset($woocommerce_loop['name']) && empty($woocommerce_loop['name']) ) ) {
        return $price;
    }

    ## Here below come your existing code ##

    
    return $price;
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
© www.soinside.com 2019 - 2024. All rights reserved.