Custom Woocommerce Shipping Calculation

Have setup woocommerce to add surcharges to shipping but I only the surcharges added to the shipping method called “Toll IPEC”. I added the foreach loop so would go through the array and if matched add the charges but its not entering the loop and adding the charges. Can someone please tell me what I’m doing wrong here?

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 Woocommerce Shipping Calculation

Leave a Reply

Your email address will not be published. Required fields are marked *