Home > OS >  How to specify Woocommerce product in a script?
How to specify Woocommerce product in a script?

Time:09-09

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' );

CodePudding user response:

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 );
  • Related