在 WooCommerce 商店和档案页面上发布可变产品的隐藏产品单一价格

Issue hide product single price for variable products on WooCommerce shop and archives pages

如果产品可变,我将使用此代码段来隐藏单一价格。工作正常但不适用于循环的第一个产品变量:

add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price' );

function hide_single_price() {
    global $product;
    if ( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    }
}

知道为什么吗?

我没有立即发现您当前的代码有问题,您可以尝试:

  • 调整优先级值
  • 回显文字 'debug'。所以你可以检查第一个产品是否显示。
function hide_single_price() {
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        if ( $product->is_type( 'variable' ) ) {
            // Debug purposes, delete afterwards!!
            echo 'DEBUG!';
            
            remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        }
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price', 5 );