function cw_apply_special_offer_shipping($params, $return)
{
    global $tables;
    $special_offers_apply =& cw_session_register('special_offers_apply');
    $products = $params['products'];
    if (empty($special_offers_apply['free_shipping'])) {
        return $return;
    }
    if (empty($return)) {
        return $return;
    }
    //cw_var_dump($return);
    $new_rates = array();
    // Re-calculate applicable total weight / items / subtotal	taking into account bonuses
    $total = $params['what_to_ship_params'];
    if (is_array($special_offers_apply['free_shipping']) && is_array($special_offers_apply['free_shipping']['products']) && !empty($special_offers_apply['free_shipping']['products'])) {
        $hash = crc32(serialize($total));
        foreach ($special_offers_apply['free_shipping']['products'] as $pid => $qty) {
            foreach ($products as $kk => $product) {
                if ($product['product_id'] == $pid) {
                    $tmp_qty = min($qty, $product['amount']);
                    if ($tmp_qty <= 0) {
                        continue;
                    }
                    # Calculate total_cost and total_+weight for shipping calculation
                    if ($product["free_shipping"] == "Y") {
                        continue;
                    }
                    if ($product['shipping_freight'] <= 0 || $config['Shipping']['replace_shipping_with_freight'] != 'Y') {
                        $total['apply']['weight'] -= $product["weight"] * $tmp_qty;
                        $total['apply']['items'] -= $tmp_qty;
                        $total['apply']['DST'] -= $product['display_discounted_price'] * $tmp_qty / $product['amount'];
                        $total['apply']['ST'] -= $product['display_subtotal'] * $tmp_qty / $product['amount'];
                        // Correct products array
                        $product['amount'] -= $tmp_qty;
                        $product['display_subtotal'] = $product['display_subtotal'] * $product['amount'] / ($tmp_qty + $product['amount']);
                        $product['display_discounted_price'] = $product['display_discounted_price'] * $product['amount'] / ($tmp_qty + $product['amount']);
                        $products[$kk] = $product;
                        if ($products[$kk]['amount'] <= 0) {
                            unset($products[$kk]);
                        }
                        $qty -= $tmp_qty;
                    }
                }
            }
        }
        if ($hash != crc32(serialize($total)) && $total['apply']['items'] > 0) {
            // Unset this function hook and retrieve rates again with corrected params
            cw_addons_unset_hooks(array('post', 'cw_shipping_get_rates', 'cw_apply_special_offer_shipping'));
            $_params = $params;
            $_params['what_to_ship_params'] = $total;
            $_params['weight'] = $total['apply']['weight'];
            $_params['products'] = $products;
            $new_rates = cw_func_call('cw_shipping_get_rates', $_params);
            // Restore function hook
            cw_addons_set_hooks(array('post', 'cw_shipping_get_rates', 'cw_apply_special_offer_shipping'));
        }
    }
    // Re-calc rates
    foreach ($return as $k => $rate) {
        // If bonus is applicable for certain methods only, then check this method
        if (!empty($special_offers_apply['free_shipping']['methods']) && is_array($special_offers_apply['free_shipping']['methods']) && !in_array($rate['shipping_id'], $special_offers_apply['free_shipping']['methods'])) {
            continue;
        }
        // If bonus is applicable for whole cart, then set the new rate regardless cart content
        if ($special_offers_apply['free_shipping']['apply'] == constant('PS_APPLY_CART')) {
            $return[$k]['saved_rate'] = $rate['original_rate'];
            // save initial rate to calc discount
            $return[$k]['original_rate'] = $special_offers_apply['free_shipping']['rate'];
            continue;
        }
        // If bonus is applicable for selected products then
        if (in_array($special_offers_apply['free_shipping']['apply'], array(PS_APPLY_COND, PS_APPLY_PRODS)) && !empty($special_offers_apply['free_shipping']['products'])) {
            if (isset($new_rates[$k])) {
                $return[$k] = $new_rates[$k];
            }
            if ($total['apply']['items'] <= 0) {
                $return[$k]['original_rate'] = 0;
            }
            $return[$k]['original_rate'] += $special_offers_apply['free_shipping']['rate'];
            $return[$k]['saved_rate'] = $rate['original_rate'];
            // save initial rate to calc discount
            if (defined('AOM') && constant('AOM')) {
                $return[$k]['shipping'] .= '*';
                //$return[$k]['shipping'] .= ' [offer: '.$rate['original_rate'].' => '.floatval($special_offers_apply['free_shipping']['rate']).'+'.floatval($new_rates[$k]['original_rate']).']';
            }
        }
    }
    // strange, but $rate['original_rate'] is exactly final rate value, not initial as you may think
    return $return;
}
<?php

/**
 * Performance improvement for customer area.
 * Unset hooks if there is only one domain specified. Filter by domain does not make sense.
 */
if (count(cw_md_get_domains()) == 1) {
    cw_addons_unset_hooks(array('pre', 'cw_product_search', 'cw_md_product_search'), array('pre', 'cw_category_search', 'cw_md_category_search'), array('pre', 'cw_manufacturer_search', 'cw_md_manufacturer_search'), array('pre', 'cw_pages_search', 'cw_md_pages_search'), array('pre', 'cw_speed_bar_search', 'cw_md_speed_bar_search'), array('pre', 'cw_shipping_search', 'cw_md_shipping_search'), array('pre', 'cw_payment_search', 'cw_md_payment_search'), array('pre', 'cw_product_get', 'cw_md_product_search'), array('pre', 'cw_category_get', 'cw_md_category_search'), array('pre', 'cw_manufacturer_get', 'cw_md_manufacturer_search'), array('pre', 'cw_pages_get', 'cw_md_pages_search'));
}