Change WooCommerce Subscriptions price strings

47 Views Asked by At

I have been able to translate most things on the site with the code below, but not for 3 fields.

"USD" "/Month" "/Month for 1 Month For".

I would like to make that say something different that will make it easier to translate later.

function wc_billing_field_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
     case '/ Month' :
     $translated_text = __( 'Monthly-Cycle', 'woocommerce' );
     break;
     case '/ Month for 1 Month For' :
     $translated_text = __( 'Monthly-Cycle for a Month', 'woocommerce' );
     break;
     case 'USD' :
     $translated_text = __( 'US-Dollar', 'woocommerce' );
     break;
     case 'First renewal' :
     $translated_text = __( 'First-Cycle', 'woocommerce' );
     break;

     }
     return $translated_text;
    }
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );
1

There are 1 best solutions below

1
LoicTheAztec On

Better try to use the following for "USD", "/Month" and "/Month for 1 Month For" strings:

add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_price_string_replacement', 10 );
add_filter( 'woocommerce_subscription_price_string', 'subscr_price_string_replacement', 10 );
function subscr_price_string_replacement( $price_string ){
    $original_string1    = "/ Month for 1 Month For";
    $original_string2    = "/ Month";
    $original_string3    = "USD";

    if ( strpos($subscription_string, $original_string1) !== false ) {
        $replacement_string = __( "Monthly-Cycle for a Month", 'woocommerce' );
        $price_string       = str_replace( $original_string1, $replacement_string, $subscription_string );
    }

    if ( strpos($subscription_string, $original_string2) !== false ) {
        $replacement_string = __( "Monthly-Cycle", 'woocommerce' );
        $price_string       = str_replace( $original_string2, $replacement_string, $subscription_string );
    }

    if ( strpos($subscription_string, $original_string3) !== false ) {
        $replacement_string = __( "US-Dollar", 'woocommerce' );
        $price_string       = str_replace( $original_string3, $replacement_string, $subscription_string );
    }
    return $price_string;
}

And for "First renewal" string better use:

add_filter( 'gettext', 'subscr_string_replacement', 20, 3 );
function subscr_string_replacement( $translated_text, $original_text, $domain ) {
    if ( $original_text === 'First renewal: %s' ) {
        $translated_text = __( 'First-Cycle: %s', 'woocommerce' );
    }
    return $translated_text;
}

Code goes in functions.php file of your child theme (or in a plugin). It should work for most strings.