Paul VI
Paul VI

Reputation: 583

Woocommerce Subscriptions Product Price String

I have a function that can change the text of the subscription details.

function wc_subscriptions_custom_price_string( $pricestring ) {
global $product;

$products_to_change = array( 2212 );

if ( in_array( $product->id, $products_to_change ) ) {
    $pricestring = str_replace( 'on the 20th day of every 6th month', 'on the 20th November and 20th May', $pricestring );
}

return $pricestring;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );

This works well - but it does not change the text in the cart or the mini cart - that still shows the default text of 20th day of every 6th month. How do I also apply this to the cart?

Upvotes: 0

Views: 3149

Answers (2)

Alex Mustin
Alex Mustin

Reputation: 43

I believe you need to use one function to apply to the Product pages (where you use global $product), and another function to apply to the Cart.

So you would need both:

//* Function for Product Pages
function wc_subscriptions_custom_price_string( $pricestring, $product, $include ) {

    global $product;

    $products_to_change = array( 2212 );

    if ( in_array( $product->id, $products_to_change ) ) {
        $pricestring = str_replace( 'on the 20th day of every 6th month', 'on the 20th November and 20th May', $pricestring );
    }

    return $pricestring;

}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );

//* Function for Cart
function wc_subscriptions_custom_price_string_cart( $pricestring ) {

    $pricestring = str_replace( 'on the 20th day of every 6th month', 'on the 20th November and 20th May', $pricestring );

    return $pricestring;

}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string_cart' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string_cart' );

Upvotes: 1

Priyanka Modi
Priyanka Modi

Reputation: 1624

Try this code,

function wc_subscriptions_custom_price_string( $pricestring ) {
global $product;

$products_to_change = array( 2212 );

if ( in_array( $product->id, $products_to_change ) ) {
    $newprice = str_replace( 'on the 20th day of every 6th month', 'on the 20th November and 20th May', $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' );

Hope that it's work !!

Upvotes: 1

Related Questions