Code Snippet to Arrange the ELEX USPS Shipping Services on the Top of Other Shipping Services

The following code snippet will help you to show shipping methods by ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce first on the cart among all other shipping methods including shipping methods by other third-party plugins.

Add the following code in the theme editor php.functions file and save the changes for implementing the requirement –

Code Snippet :

 
add_filter( 'woocommerce_package_rates' , 'elex_sort_shipping_services_by_name', 10, 2 );
function elex_sort_shipping_services_by_name( $rates, $package ) 
{
    if ( ! $rates ) return $rates;
    
    $carrier_sort_array = array(
        'wf_usps_stamps',
    );
    $sort_rates_temp = array();
    foreach( $rates as $carrier_id => $rate ) {
        
        foreach($carrier_sort_array  as $carrier_sort_name){
            $carrier_name = current(explode(":",$carrier_id));
            if($carrier_name == $carrier_sort_name){			
                $sort_rates_temp[$carrier_sort_name][$carrier_id] = $rate;
                break;
            }
        }
    }
    $sort_rates = array();
    foreach($carrier_sort_array as $carrier_sort_name){
        if(isset($sort_rates_temp[$carrier_sort_name]) && !empty($sort_rates_temp[$carrier_sort_name])){
            $sort_rates = array_merge($sort_rates,$sort_rates_temp[$carrier_sort_name]);
        }
        
    }
    $rates = array_merge($sort_rates,$rates);

    return $rates;
}

Check how to paste the code snippet and make the changes in the required path:

Copy and paste the code snippet in the below path and, on the end of the activated theme on your site’s php.functions file in the same way as shown in the below screenshot. Then, update the file.

Path : Storefront: Theme Functions (functions.php)

Code Snippet to arrange ELEX USPS Shipping carriers on the top of other shipping carriers | code snippet

The Cart page would look like the below screenshot before applying the code snippet:

Code Snippet to arrange ELEX USPS Shipping carriers on the top of other shipping carriers | normal shipping service arrangement

After we apply the code snippet, the Cart page will change as seen in the below screenshot:

Code Snippet to arrange ELEX USPS Shipping carriers on the top of other shipping carriers | listing USPS service on top

Please contact the support team to troubleshoot any issue related to the same.


To know more about ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce, please visit the product page.

To know more about setting up the plugin, read the product documentation.

 

Next How to Hide Shipping Methods Based On Destination Countries (US & Canada)? – Code Snippet

    Leave a Reply

    WordPress Image Lightbox Plugin