Wordpress how to edit yith subscription reccuing price on cart/checkout

I am doing custom coding for adding a product to the cart with a custom price ( Subscription Product type from YITH ) based on a condition ( qty of sensors products in the cart ).

Now I can edit the initial price with the below hook ( First month period ) the problem is that the recurring price still gets the default price, not the one I determine on the hook.

so is there a way to edit the recurring price for the YITH Subscription plugin

add_action('woocommerce_before_calculate_totals', 'add_product_to_cart_on_update');

function add_product_to_cart_on_update($cart)
{
    if (is_admin() && ! defined('DOING_AJAX')) {
        return;
    }
    
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    
    $product_id_to_add = get_field('subscription_product', 'option');

    // $product_id_to_add = 767; // The product ID to add
    $qty = 0;
    $found = false;
    $product_cart_key_to_add = '';

    $sensor_products = array();

    // Loop through the cart to gather data
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];
        $product_qty = $cart_item['quantity'];
        $is_sensor_product = get_post_meta($product_id, '_sensor_product', true);

        if ($is_sensor_product === 'yes') {
            $sensor_products[$cart_item_key]['id'] = $product_id;
            $sensor_products[$cart_item_key]['qty'] = $product_qty;
        }

        if ($product_id_to_add == $product_id) {
            $found = true;
            $product_cart_key_to_add = $cart_item_key;
        }
    }

    // Calculate the total quantity of sensor products
    foreach ($sensor_products as $sensor_p) {
        $qty += $sensor_p['qty'];
    }

    // Get the product object
    $product = wc_get_product($product_id_to_add);
    
    $sale_qty = get_field('number_of_sensors', 'option');

    if ( $sale_qty != null && $qty < $sale_qty && $found) {
        // Set the price to the regular price if quantity is less than 5
        $regular_price = $product->get_regular_price();
        $cart->set_quantity($product_cart_key_to_add, $qty);
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_price($regular_price);
    } elseif (!$found) {
        // Add the product with the default WooCommerce price handling
        $cart_item_key = $cart->add_to_cart($product_id_to_add, $qty);
        $product_cart_key_to_add = $cart_item_key;
        $regular_price = $product->get_regular_price();
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_price($regular_price);
    } else {
        // If quantity is 5 or more, keep the sale price by default
        $cart_item_key = $cart->add_to_cart($product_id_to_add, $qty);
    }
}

I’d be glad to help you with that. Here’s the revised code that addresses the issue of setting the recurring price for the YITH Subscription plugin:

PHP

add_action('woocommerce_before_calculate_totals', 'add_product_to_cart_on_update');

function add_product_to_cart_on_update($cart)
{
    if (is_admin() && ! defined('DOING_AJAX')) {
        return;
    }

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $product_id_to_add = get_field('subscription_product', 'option');

    // $product_id_to_add = 767; // The product ID to add
    $qty = 0;
    $found = false;
    $product_cart_key_to_add = '';

    $sensor_products = array();

    // Loop through the cart to gather data
    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        $product_id = $cart_item['product_id'];
        $product_qty = $cart_item['quantity'];
        $is_sensor_product = get_post_meta($product_id, '_sensor_product', true);

        if ($is_sensor_product === 'yes') {
            $sensor_products[$cart_item_key]['id'] = $product_id;
            $sensor_products[$cart_item_key]['qty'] = $product_qty;
        }

        if ($product_id_to_add == $product_id) {
            $found = true;
            $product_cart_key_to_add = $cart_item_key;
        }
    }

    // Calculate the total quantity of sensor products
    foreach ($sensor_products as $sensor_p) {
        $qty += $sensor_p['qty'];
    }

    // Get the product object
    $product = wc_get_product($product_id_to_add);

    $sale_qty = get_field('number_of_sensors', 'option');

    if ( $sale_qty != null && $qty < $sale_qty && $found) {
        // Set the price to the regular price for both initial and recurring payments
        $regular_price = $product->get_regular_price();
        $cart->set_quantity($product_cart_key_to_add, $qty);
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_price($regular_price);
        // Set the recurring price
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_recurring_price($regular_price);
    } elseif (!$found) {
        // Add the product with the default WooCommerce price handling
        $cart_item_key = $cart->add_to_cart($product_id_to_add, $qty);
        $product_cart_key_to_add = $cart_item_key;
        $regular_price = $product->get_regular_price();
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_price($regular_price);
        // Set the recurring price
        $cart->get_cart_item($product_cart_key_to_add)['data']->set_recurring_price($regular_price);
    } else {
        // If quantity is 5 or more, keep the sale price by default
        $cart_item_key = $cart->add_to_cart($product_id_to_add, $qty);
    }
}

Explanation of changes:

  • Set recurring price:
    • After setting the initial price, the code now also sets the recurring price to the same value using $cart->get_cart_item($product_cart_key_to_add)['data']->set_recurring_price($regular_price); .
    • This ensures that both the initial and recurring prices are updated based on the specified condition.

This revised code should effectively set both the initial and recurring prices for the YITH Subscription product based on your custom condition, ensuring the correct pricing is applied.