如何更改 WooCommerce 订阅中的价格字符串

Use*_*342 3 wordpress woocommerce woocommerce-subscriptions

我需要一个更好的方法来做到这一点。

目前,我已将代码直接添加到 class-wc-subscriptions-product.php 文件中的 get_price_string 函数中,因此当设置免费试用版时,我可以更改添加到价格字符串中的文本。

当然,这远非完美。

那么,有谁知道我需要将什么钩子添加到主题文件夹中的functions.php 文件中才能执行相同的操作?

MrC*_*rot 5

好的老问题,但我最近需要这样做。我不想依赖字符串替换,所以这就是我的做法(可以根据您的需求进行调整 - 关键是查看$product您可以使用的属性):

add_filter( 'woocommerce_subscriptions_product_price_string', 'my_subs_price_string', 10, 3 );

function my_subs_price_string( $subscription_string, $product, $include ) {

    /****
    var_dump($product); Various variables are available to us
    ****/

    return 'An initial easy payment of ' . wc_price( $product->subscription_sign_up_fee ) . 
        ', a ' . $product->subscription_trial_length . ' ' . $product->subscription_trial_period . 
        ' trial of the product, then an outright purchase of ' . wc_price( $product->subscription_price );
}
Run Code Online (Sandbox Code Playgroud)