'Don't show recurring price for WooCommerce subscriptions worth 0$

I want to modify the subscription string so it only shows the sign-up fee for variations with 0$ recurring fee.

I would also like to show that variation price in the archive for that product rather than a price range.

I've tried using the str_replace function and it worked for everything besides this specific string. Besides, I am unable to select specifically the strings with 0$ recurring fees.

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

The expected output would be only the signup price with the price string replaced with nothing



Solution 1:[1]

The variable $subscription_string will contain the HTML code for the currency amount and symbol. So the direct string replace will not work here with the text you provided.

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mujuonly