Support » Plugin: WooCommerce UPS Shipping – Live Rates and Access Points » Set a minimum shipping cost

  • Resolved afoc

    (@afoc)


    Is there a way to set a minimum shipping cost using this plugin? I’d like to set a minimum rate, where if the plugin returns a value lower then this, it gets bumped up to that minimum. If the values returned are above this, they remain the same.

    Example, I set minimum to $15

    UPS 2nd Day Air: $31.12
    UPS Ground: $10.83 .. this becomes $15

    UPS 2nd Day Air: $45.68
    UPS Ground: $18.25 .. shipping cost above $15 is ok as is

    Note this is not a fallback nor is it a flat rate. It’s a threshold that all shipping costs must be at or above. If this isn’t possible out of the box, I’m likely to modify the plugin so I’d also be happy to save some time if you could point me to where this would best be done in the code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Marta Pawlonka

    (@martapaw)

    Hello @afoc,

    Thank you for your message.

    We’ll work on this, so I’ll let you know when we’ll provide this feature.

    Best regards,
    Marta

    Thread Starter afoc

    (@afoc)

    Ok, thank you for the quick response.

    David

    (@dlim_vernier)

    Here’s a code snippet you could add to your functions.php or create a plugin with the following:

    You’ll need to see which UPS methods you use to find the IDs.

    // Hat tip: https://gist.github.com/xadapter/c91282b9e1df7623085745fd9008bf1c
    add_filter( ‘woocommerce_package_rates’, function( $shipping_costs) {
    $shipping_method_min_cost = array(
    // Shipping id => min_cost
    ‘ups:1:03’ => 11,
    ‘ups:1:12’ => 11,
    ‘ups:1:02’ => 11,
    ‘ups:1:13’ => 11,
    ‘ups:1:1’ => 11,
    );

    foreach( $shipping_costs as $shipping_cost ) {
    $shipping_method_id = $shipping_cost->get_id();
    if( isset($shipping_method_min_cost[$shipping_method_id]) ) {
    $cost = (float) $shipping_cost->get_cost();
    if( $cost < $shipping_method_min_cost[$shipping_method_id] ) {
    $shipping_cost->set_cost($shipping_method_min_cost[$shipping_method_id]);
    }
    }
    }
    return $shipping_costs;
    });

    Thread Starter afoc

    (@afoc)

    Thanks @dlim_vernier, that was helpful.

    The IDs can be checked by echoing the contents of $package['rates'] in plugins/woocommerce/includes/class-wc-shipping.php, which is where the woocommerce_package_rates filters are applied.

    I found that the method IDs on my local environment are named like this: flexible_shipping_ups:2:01, etc, but they weren’t the same for the production environment. I ended up doing the following & setting a minimum no matter what method is used.

    
    add_filter( 'woocommerce_package_rates', function( $shipping_costs, $package ) {
      $shipping_min_cost = 15;
    
      foreach( $shipping_costs as $shipping_cost ) {
        $shipping_method_id = $shipping_cost->get_id();
        $cost = (float) $shipping_cost->get_cost();
        if( $cost < $shipping_min_cost ) {
          $shipping_cost->set_cost($shipping_min_cost);
        }
      }
      return $shipping_costs;
    }, 1, 2);
    

    @afoc
    Thanks afoc..
    I use this snippet for some months now, but I saw some days ago that the amount of the VAT on the shipping cost is calculate on the previous value.
    Any idea why?
    Thanks
    Example:
    Total = 119
    UPS = 9.88 (the minimum value)
    Tax (20%) = 25.57 – I must have 25.78
    Total = 154.45 – I must have 154.66

    
    add_filter( 'woocommerce_package_rates', function( $shipping_costs, $package ) {
    	// Bellow minimum cost:
    	$shipping_min_cost = 9.88;
    	foreach( $shipping_costs as $shipping_cost ) {
    		$shipping_method_id = $shipping_cost->get_id();
    		$cost = (float) $shipping_cost->get_cost();
    		// 2 lines bellow added by Remi:
    		$cost = $cost*1.11;
    		$shipping_cost->set_cost($cost);
    		if( $cost < $shipping_min_cost ) {
    			$shipping_cost->set_cost($shipping_min_cost);
    		}
    	}
    return $shipping_costs;
    }, 1, 2);
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Set a minimum shipping cost’ is closed to new replies.