Move WooCommerce shipping method to the end of the list on frontend with PHP

This is useful if you have old shipping methods in WooCommerce (i.e. those that are not added to shipping zones).

add_filter( 'woocommerce_package_rates' , 'wpcb_move_wc_shipping_method_to_the_end', PHP_INT_MAX, 2 );
if ( ! function_exists( 'wpcb_move_wc_shipping_method_to_the_end' ) ) {
    /**
     * wpcb_move_wc_shipping_method_to_the_end.
     * 
     * @see https://wpcodebook.com/snippets/move-woocommerce-shipping-method-to-the-end-of-the-list-on-frontend-with-php/
     */
    function wpcb_move_wc_shipping_method_to_the_end( $rates, $package ) {
        if ( ! $rates ) {
            return $rates;
        }
        $shipping_method_id_to_move = 'flat_rate'; // Important: change this to your method's ID
        if ( isset( $rates[ $shipping_method_id_to_move ] ) ) {
            $saved_rate = $rates[ $shipping_method_id_to_move ];
            unset( $rates[ $shipping_method_id_to_move ] );
            $rates[ $shipping_method_id_to_move ] = $saved_rate;
        }
        return $rates;
    }
}

No comments yet.

Leave a Reply