skip to Main Content

I have multiple subscription products in my website with monthly, yearly and weekly.

I am running this code which is working fine (changing “month to 30 days “) but only works for the products with “Monthly” how can I apply it to change the the string for weekly and yearly also?

Thank you

function wc_subscriptions_custom_price_string( $pricestring ) {
    $newprice = str_replace( 'month', '30 days', $pricestring );
    return $newprice;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );

2

Answers


  1. You can choose to replace all strings you need given that you know, what it says right now.

    I can’t find the documentation, but given that the $pricestring takes on values biweekly, weekly, yearly, I would follow the pattern, and try:

    function wc_subscriptions_custom_price_string( $pricestring ) {
        $newprice = str_replace( 'biweekly', '14 days', $pricestring );
        $newprice = str_replace( 'weekly', '7 days', $pricestring );
        $newprice = str_replace( 'yearly', '365 days', $pricestring );
        return $newprice;
    }
    

    Basically, it takes the $pricestring and replaces whatever the text (the first parameter) found inside, and replaces it with whatever text (the second parameter), saving it again and returning the modified $newprice.

    Notice the order of biweekly and weekly if you want to avoid accidentally rewriting weekly in biweekly.

    Login or Signup to reply.
  2. Just do similar replaces, you can even embed them, like:

    function wc_subscriptions_custom_price_string( $pricestring ) {
        $newprice = str_replace(
            'month', 
            '30 days', 
            str_replace(
                'year',
                '365 days',
                str_replace(
                    'week',
                    '7 days',
                    $pricestring
                )
            ) 
        );
        return $newprice;
    }
    add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
    add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search