Search code examples
phpwordpresswoocommercereplacewoocommerce-subscriptions

Change in single product page WooCommerce subscription price string


In WooCommerce, I am using WooCommerce subscriptions plugin, and I'm trying to replace from the subscription price string the text "every 3 days" with "Twice a Week" in single product pages.

This is my code attempt:

function replace_single_product_table_text( $content ) {

    if ( is_product() ) {

        preg_match_all( '/<table[^>]*class="[^"]*shop_table[^"]*"[^>]*>(.*?)<\/table>/s', $content, $matches );


        foreach ( $matches[0] as $match ) {
            $new_content = str_replace( 'every 3 days', 'Twice a Week', $match );
            $new_content = str_replace( '/ week', '/ Week', $new_content );
            $new_content = str_replace( 'one a Week', 'Once a Week', $new_content );
            $content = str_replace( $match, $new_content, $content );
        }
    }
    return $content;
}
add_filter( 'the_content', 'replace_single_product_table_text', 99 );

Unfortunately, it doesn't work at all.

Any help will be appreciated.


Solution

  • You are not using the wright hook and your code can be simplified. Try the following:

    add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_product_price_string_replacement', 10, 3 );
    function subscr_product_price_string_replacement( $subscription_string, $product, $include ){
        $original_string    = "every 3 days";
        $replacement_string = "Twice a Week";
    
        if ( strpos($subscription_string, $original_string) !== false ) {
            $subscription_string = str_replace( $original_string, $replacement_string, $subscription_string );
        }
        return $subscription_string;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.