0

I have the below function adding surcharges to shipping

add_filter( 'woocommerce_package_rates',  
'add_shipping_percentage_surcharge', 10, 2 );
function add_shipping_percentage_surcharge( $rates, $package ) {
     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;
}

What I need is to only add these charges to one shipping method called "Toll IPEC". I found code below the lines but when putting it in the function with the if statement its not entering it.

$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0];
if ($chosen_shipping =='Toll IPEC')

I have placed the shipping method as first method in the shipping zone but it doesn't seem to be working?

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • [php var_dump() vs print_r()](http://stackoverflow.com/q/3406171/1287812) and [PHP: for](http://php.net/manual/es/control-structures.for.php) – brasofilo May 09 '17 at 11:58
  • @user3727761 Try making the filter priority as dead last i.e. `add_filter( 'woocommerce_package_rates', 'add_shipping_percentage_surcharge', 999, 2 );`. This should override anything else. – adamj May 10 '17 at 04:19
  • Am I don't think that would make a difference as I only want to apply the filter to one shipping method? – user3727761 May 10 '17 at 07:40
  • This is hat I think it should do but its not working, when I print its not showing up on the checkout page so cant tell if its right or not... $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; } } } } return $rates; } – user3727761 May 11 '17 at 13:13

0 Answers0