0

I want to change the text and the order of my price string in WooCommerce Subscriptions. Right now it says:

“$35.00 on the 1st of each month for 11 months and a $35.00 sign-up fee.”

and I want it to say:

“$35.00 for the first box and then $35.00 on the 1st of each month for 11 months.”

I found this code that I can use to change "sign-up fee" to "for the first box":

 /* WooCommerce Subscriptions Price String */

 function wc_subscriptions_custom_price_string( $pricestring ) {
    $newprice = str_replace( 'sign-up fee', 'for the first box', $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' );

Now it says “$35.00 on the 1st of each month for 11 months and a $35.00 for the first box.”

How can I change the order?

Matt
  • 211
  • 4
  • 16

1 Answers1

1

Simply reorder the string by exploding the original into an array:

function wc_subscriptions_custom_price_string( $pricestring ) {
   $replace_price = str_replace( 'sign-up fee', 'for the first box', $pricestring );
   $aPrice = explode(" and a ", $replace_price);
   $newprice = $aPrice[1] . " and then " . $aPrice[0]; 
   $finalprice = str_replace(" on "," +shipping on ", $newprice);
   return finalprice;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );

See: explode()

Or if you want to get fancy:

$newprice = implode(" and then ", array_reverse($aPrice));
Jamie_D
  • 979
  • 6
  • 13
  • Ok this worked great but I had a slight change after speaking with the client. They would like it to say "$35.00 for the first box and then $35.00 +shipping on the 1st of each month for 11 months. – Matt Nov 09 '18 at 19:12
  • How can we add '+ shipping' between the price and the "on the 1st of each..."? – Matt Nov 09 '18 at 19:13