skip to Main Content

I’m trying to add a 65% increase across all shipping methods.

I’ve used variations of this function but this doesn’t affect the rates that are returned.

function increase_shipping_costs() {
  // Get the current shipping rates
  $shipping_zones = WC_Shipping_Zones::get_zones();

  foreach ( $shipping_zones as $zone ) {
    foreach ( $zone['shipping_methods'] as $shipping_method ) {
      // Check if the shipping method is a flat rate
      if ( !$shipping_method->id === 'free_shipping' ) {
        // Get the current cost
        $current_cost = $shipping_method->cost;

        // Calculate the increased cost
        $increased_cost = $current_cost + ( $current_cost * 0.65 );

        // Update the shipping cost
        $shipping_method->cost = $increased_cost;
        $shipping_method->save();
      }
    }
  }
}

add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);

2

Answers


  1. The filter’s callback is not setup properly:

    • function parameters are missing
    • no value being returned (filters require a return value)

    You’ll want to use the data provided through the filter (ex: $package_rates) rather than WC_Shipping_Zones::get_zones().

    function increase_shipping_costs( $package_rates, $package ) {
        ...
    
        return $package_rates;
    }
    add_filter( 'woocommerce_package_rates', 'increase_shipping_costs', 10, 2 );
    
    Login or Signup to reply.
  2. To add a 65% increase cost across all shipping methods rates, woocommerce_package_rates is the correct filter hook to be used, but you will need to modify mostly everything in your function to get it working properly (as there are some missing things and some mistakes in your code):

    add_filter('woocommerce_package_rates', 'increase_shipping_costs', 10, 2);
    function increase_shipping_costs( $rates, $package ) {
        $rate_multiplier = 1.65; //  65% increase cost
        
        // Loop through shipping rates
        foreach ( $rates as $rate_key => $rate ) {
            
            if ( 'free_shipping' !== $rate->method_id && $rate->cost > 0 ) {
                $has_taxes = false; // Initializing
                $taxes     = array(); // Initializing
    
                $rates[$rate_key]->cost = $rate->cost * $rate_multiplier; // Set new rate cost
                
                // Loop through taxes array (change taxes rate cost if enabled)
                foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $taxes[$key] = $tax * $rate_multiplier; // Set the new tax cost in the array
                        $has_taxes   = true; // Enabling tax changes
                    }
                }
                // set array of shipping tax cost
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
            }
        }
        // For a filter hook, always return the main argument
        return $rates; 
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search