Have setup WooCommerce to add surcharges to shipping methods, but I only want the surcharges added to the shipping method called “Toll IPEC”. I have added a foreach loop so can go through the array and if shipping method matches, I add a surcharge. But it’s not working in that loop and not adding a surcharge.

Can someone please tell me what I’m doing wrong here?

Here is my code:

add_filter( 'woocommerce_package_rates', 'add_shipping_percentage_surcharge', 10, 2 );
function add_shipping_percentage_surcharge( $rates, $package ) {

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );

    foreach( $chosen_methods as $key => $value1) {
        if ($value1 =='Toll IPEC')
            foreach( $rates as $key => $value ) {
                if( $value->method_id == 'betrs_shipping' ) {
                    $rates[ $key ]->cost += $rates[ $key ]->cost * 0.12;
                    $rates[ $key ]->cost += $rates[ $key ]->cost * 0.10;
                    $rates[ $key ]->cost += $rates[ $key ]->cost * 0.30;
    return $rates;

Read more here: Custom Calculation on WooCommerce Shipping methods


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: