【问题标题】:Don't show recurring price for WooCommerce subscriptions worth 0$不显示价值 0 美元的 WooCommerce 订阅的经常性价格
【发布时间】:2022-04-16 20:35:07
【问题描述】:

我想修改订阅字符串,使其仅显示 0$ 经常性费用的变体的注册费。

我还想在存档中显示该产品的变化价格,而不是价格范围。

我已经尝试使用 str_replace 函数,它适用于除此特定字符串之外的所有内容。此外,我无法专门选择 0$ 经常性费用的字符串。

$subscription_string = str_replace('$0.00 / month and a ', '', $subscription_string)

预期的输出将只是价格字符串替换为任何内容的注册价格

【问题讨论】:

  • 它对我来说效果很好 - 请参阅this screenshot。您的 $subscription_string 是否有可能在文本中包含大写字母? str_replace 默认区分大小写。

标签: php wordpress woocommerce hook-woocommerce woocommerce-subscriptions


【解决方案1】:

变量$subscription_string 将包含货币金额和符号的HTML 代码。因此,直接字符串替换不适用于您提供的文本。

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

function alter_woocommerce_subscriptions_product_price_string($subscription_string, $product, $include) {
    if ($include['sign_up_fee']) {
        if (strpos($subscription_string, '0.00') !== false) {
            $subscription_string = explode('and a', $subscription_string);
            $subscription_string = $subscription_string[1];
        }
    }
    return $subscription_string;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2021-07-22
    • 2015-06-10
    • 2021-04-10
    • 2017-05-15
    • 2017-02-01
    • 2020-04-29
    相关资源
    最近更新 更多