0

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' );
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Hi @Imran! What is your attempt on solving the problem? Can you post what you have tried so far? Do you know what string you want to replace with what? Thank you! – Kit Ostrihon Dec 18 '19 at 00:19

2 Answers2

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.

Kit Ostrihon
  • 824
  • 2
  • 14
  • 36
  • Thanks for your coment @Kit O, I did the same previously but it's working only for 1 parameter not all three. – Imran Irshad Dec 18 '19 at 03:24
  • Not sure, what do you mean by that. I think you essencially need to know, what is the current value, to be able to replace it. Maybe you can have a read on https://laurena.blog/collecting-donations-with-woocommerce/ then, and see if there is something helpful. Thank you! – Kit Ostrihon Dec 18 '19 at 06:19
  • Looking at the article and the documentation: https://docs.woocommerce.com/document/customers-extend-subscription-expiration-date/ it looks replacing the last word should work. Have you tried typing in `"week"` and `"year"`, @ImranIrshad? – Kit Ostrihon Dec 18 '19 at 06:30
0

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' );
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175