php - 不要显示值(value) 0 美元的 WooCommerce 订阅的经常性价格

标签 php wordpress woocommerce hook-woocommerce woocommerce-subscriptions

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

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

我试过使用 str_replace 函数,它适用于除这个特定字符串之外的所有内容。此外,我无法具体选择经常性费用为 0 美元的字符串。

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

预期的输出将只是注册价格,价格字符串没有被替换

最佳答案

变量 $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;
}

关于php - 不要显示值(value) 0 美元的 WooCommerce 订阅的经常性价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56192639/

相关文章:

Wordpress wp_editor 简码问题

css - 在 WordPress 中使用多个样式表

php - Google Analytics Reporting API V4 中的结果为零?

php - 警告 : Illegal string offset - PHP mySQL

php - FirePhp 是否与 FireBug 1.12.0 兼容?

PHPExcel 将 xls DATETIME 更改为数字

php - Wordpress: get_attached_media ('image' ) 按标题排序

php - 为 WooCommerce 中的特定结账字段启用更新结账

php - 在 woocommerce 中添加自定义产品类型

wordpress - WooCommerce [product_categories] 短代码 - 缺少显示的子类别