通过WooCommerce产品变体上的文字替换空白和零显示价格

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

我需要争论。 $blank_price$zero_price。如果产品价格留空(未填写),产品页面将显示$blank_price参数的值,如果产品价格设置为0,则应显示$zero_price参数中的值。

我需要这个适用于简单和可变产品(变体)。

这是我到目前为止,它适用于简单的产品。我需要帮助添加变量产品的代码。

代码:

add_filter( 'woocommerce_get_price_html', 'empty_and_zero_price_display', 20, 2 );
function empty_and_zero_price_display( $price, $product ) {
    $blank_price = __('Price not yet set', 'woocommerce');
    $zero_price = __('Free', 'woocommerce');

    if( $product->is_type('simple') ) {
        if ( '' === $product->get_price() ) {
            return $blank_price;
        } else if (0 == $product->get_price() ) {
            return $zero_price;
        }
    }
    return $price;
}

任何帮助表示赞赏。

php wordpress woocommerce price variations
1个回答
0
投票

为了使其适用于变量产品的变体,请尝试这个轻微更改的代码:

add_filter( 'woocommerce_get_price_html', 'empty_and_zero_price_display', 20, 2 );
function empty_and_zero_price_display( $price, $product ) {
    $blank_price = __('Price not yet set', 'woocommerce');
    $zero_price = __('Free', 'woocommerce');

    if( ! $product->is_type('variable') ) {
        if ( '' === $product->get_price() ) {
            return $blank_price;
        } else if (0 == $product->get_price() ) {
            return $zero_price;
        }
    }
    return $price;
}

如果您想更改可变产品的显示价格范围,那就不同了,您应该提供一些与您希望如何显示相关的详细信息(在所有不同的案例组合中)。

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

enter image description here


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