WooCommerce Pro Shipping lets you set up multiple different rates, each of which can have different pricing rules, availability rules, destinations and minimum / maximum thresholds. This means that WooCommerce’s built in default “default shipping” choices – which only let you choose “Pro Shipping” aren’t quite sufficient.

However, WooCommerce does include a filter that can be used to set a default shipping choice. In our example, we’ve got a number of different rates set up.

Screenshot 2015-04-27 18.35.23

 

We’re going to make WooCommerce choose “Value Pricing” (rate ID 4) as the default if it’s available, otherwise falling back to “Quantity Pricing” (rate ID 3), or just WooCommerce’s normal “cheapest rate” choice if neither are available. The following code should be added to your theme’s functions.php file or a functionality plugin.

function lw_woocommerce_shipping_chosen_method( $method, $available_methods ) {
    // Don't do anything if a method is already selected.
    if ( ! empty( $method ) ) {
        return $method;
    }
    // Get the IDs of the methods that are available.
    $methods = array_keys( $available_methods );
    // Our preferred methods. The first method in this list that is
    // available will be defaulted.
    $preferences = array(
        'pro-shipping-4-0', // pro-shipping-{rate_id}-0
        'pro-shipping-3-0',
    );
    // Search the preferred options and return it if it's available.
    foreach ( $preferences as $preference ) {
        if ( in_array( $preference, $methods ) ) {
            return $preference;
        }
    }
    return $method;
}
add_filter( 'woocommerce_shipping_chosen_method', 'lw_woocommerce_shipping_chosen_method', 10, 2 );

This entry was posted in .
Bookmark the permalink.