Applying New Shipping Method At Checkout

I did a addon which changes shipping rate, but it doesn't work after upgrading to MVE 4.10.4SP1.

This is the part of previosly code:


fn_save_cart_content($cart, $auth);
$cart[‘change_cart_products’] = true;

fn_calculate_cart_content($cart, $auth, ‘E’, true, ‘F’, true);

fn_checkout_update_shipping($cart, array($cart[‘shipping_id’]));

if (isset($cart[‘payment_id’])) {
$cart[‘payment_method_data’] = fn_get_payment_method_data($cart[‘payment_id’]);

// Update shipping cost
$cart[‘stored_shipping’] = array();
if (!empty($cart[‘product_groups’])) {
foreach ($cart[‘product_groups’] as $group_key => $group) {
if (!empty($group[‘chosen_shippings’])) {
foreach ($group[‘chosen_shippings’] as $shipping_key => $shipping) {
$cart[‘stored_shipping’][$group_key][$shipping_key] = (float) 0;
$cart[‘product_groups’][$group_key][‘chosen_shippings’][$shipping_key][‘rate’] = 0;

$cart[‘recalculate’] = false;
$cart[‘calculate_shipping’] = false;

fn_calculate_cart_content($cart, $auth, ‘S’, true, ‘F’, true);

Now I need to fix it.

I want to apply a new shipping method manually while checkout. Not changing only rate, I want to change/apply a new shipping method.

1. Which function(s) should I use?

2. Should I change shipping method before or after fn_calculate_cart_content function?

Any idea?