Search code examples
phpwoocommercewoocommerce-subscriptions

WooCommerce Subscription Price String Duplication issue


I am developing a site that is using YayCurrency to allow USD and CAD purchases, but the plugin is duplicating the subscription pricing string. I've been able to hide the duplication in a couple areas using css, but I just want to be able to CHANGE the pricing string programmatically to say the price is the correct currency and then "monthly" instead of all the stupid "now, and $XX on the first of each month"

Here is a page where the duplication is happening: https://memberships.iwanttocraft.com/shop/

Code that has been attempted:

This technically works, but I don't want period fully removed, rather I need to to be simplified...

add_filter( 'woocommerce_subscriptions_product_price_string_inclusions', 'wpd_override_subscription_price_string', 10, 2 );
function wpd_override_subscription_price_string( $include, $product ) {

    $include['subscription_period'] = false;

    return $include;

}

This works to get me to "$XX now, and $XX monthly" but I'd also like to remove the full "now, and ...":

add_filter( 'woocommerce_subscriptions_product_price_string', 'wpd_change_subscription_product_string', 10, 3 );
function wpd_change_subscription_product_string( $subscription_string, $product, $include ) {
if ( $include['subscription_period'] ){

$current_string = 'on the 1st of each month';
$new_string = 'monthly';
$subscription_string = str_replace($current_string, $new_string, $subscription_string);

}

return $subscription_string;

}

Solution

  • To remove "$XX now, and " substring from "$XX now, and $XX monthly" string, use the following code replacement:

    add_filter( 'woocommerce_subscriptions_product_price_string', 'wpd_change_subscription_product_string', 10, 3 );
    function wpd_change_subscription_product_string( $subscription_string, $product, $include ) {
        if ( ! $include['subscription_period'] ) return $subscription_string;
    
        $sub_string_1 = 'on the 1st of each month';
    
        if ( strpos( $subscription_string, $sub_string_1 ) !== false ) {
            $subscription_string = str_replace( $sub_string_1, 'monthly', $subscription_string );
        }
    
        $sub_string_2 = 'now, and ';
        $position     = strpos( $subscription_string, $sub_string_2 );
    
        if ( $position !== false ) {
            $offset = $position + strlen( $sub_string_2 );
    
            $subscription_string = substr($subscription_string, $offset);
        }
        return $subscription_string;
    }
    

    But if you want to remove just "now, and " substring from "$XX now, and $XX monthly" string, use the following code replacement:

    add_filter( 'woocommerce_subscriptions_product_price_string', 'wpd_change_subscription_product_string', 10, 3 );
    function wpd_change_subscription_product_string( $subscription_string, $product, $include ) {
        if ( ! $include['subscription_period'] ) return $subscription_string;
    
        $sub_string_1 = 'on the 1st of each month';
    
        if ( strpos( $subscription_string, $sub_string_1 ) !== false ) {
            $subscription_string = str_replace( $sub_string_1, 'monthly', $subscription_string );
        }
    
        $sub_string_2 = 'now, and ';
    
        if ( strpos( $subscription_string, $sub_string_2 ) !== false ) {
            $subscription_string = str_replace( $sub_string_2, '', $subscription_string );
        }
        return $subscription_string;
    }
    

    Both should work.