-2

This code works for me but it makes adjustments to every subscription on the website. I want to target a particular product or array of products so others are unaffected. that's issue I have with that code on other subscriptions

add_filter( 'woocommerce_subscriptions_product_price_string', 'my_subs_price_string1', 10, 3 );

function my_subs_price_string1( $subscription_string, $product, $include ) {

    /****
    var_dump($product); Various variables are available to us
    ****/

    return 'Today ' . wc_price( $product->subscription_sign_up_fee ) . 
        ', a ' . $product->subscription_trial_length . ' ' . $product->subscription_trial_period . 
        ' trial of the product, then an outright purchase of ' . wc_price( $product->subscription_price );
}

EDIT: I tried using a snippet from the post in the answer below. But for some reason, it just removes the string and doesn't replace chosen string. Here's the snippet.

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' );
Andy
  • 25
  • 4
  • You can find your answers here, there are examples to apply the changes for specific products only https://stackoverflow.com/questions/48146090/woocommerce-subscriptions-product-price-string – Vijay Hardaha Sep 08 '22 at 23:38
  • @VijayHardaha Tried applying the code. Not even modifying. Tried replacing the string "$99.00 for 1 week with a 3-day free trial and a $1.00 sign-up" to "1 today 99 in a week." and it removed the whole string and didn't replace anything. (I deactivated my initial snippet). – Andy Sep 09 '22 at 01:29
  • Change `$newprice` to `$pricestring` and replace `$product->id` to `$product->get_id()` – Vijay Hardaha Sep 09 '22 at 03:01
  • @VijayHardaha In the first code or the second. I accidentally pasted the wrong code initially. I edited the top one. It changes all the products instead of 1. And the second one removes string all together instead of replacing. – Andy Sep 09 '22 at 03:36

1 Answers1

0

Try this clean code:

/**
 * Modify subscription product price string.
 *
 * @param string     $subscription_string   Subscription price text.
 * @param WC_Product $product               WC Product object.
 * @return string
 */
function custom_wc_subscriptions_product_price_string( $subscription_string, $product ) {
    // If not product then return original string.
    if ( ! $product ) {
        return $subscription_string;
    }

    // Get the product id.
    $product_id = $product->get_id();

    // Define allowed products ids in array comma seperated.
    $allowed_products = array( 1001, 1002, 1003 );

    // If allowed products is not empty and product is is within allowed products then only run the inner code.
    if ( ! empty( $allowed_products ) && in_array( $product_id, $allowed_products, true ) ) {
        $subscription_string = sprintf(
            /* translators: 1: Sign-up Fee 2: Trail Length 3: Trail Period 4: Subscription Price. */
            __( 'Today %1$s, a %2$s %3$s trial of the product, then an outright purchase of %4$s', 'default' ),
            wc_price( $product->subscription_sign_up_fee ),
            $product->subscription_trial_length,
            $product->subscription_trial_period,
            wc_price( $product->subscription_price )
        );
    }

    return $subscription_string;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'custom_wc_subscriptions_product_price_string', 10, 2 );
Vijay Hardaha
  • 2,411
  • 1
  • 7
  • 16