What can we help you with?
How to hide or disable a WooCommerce shipping service? (Code Snippets)
Code Snippet 01: Hide a shipping method for a particular Shipping Class in USPS Plugin
- Add the following code to your functions.php or anywhere relevant.
- You need to add either shipping_class_ids of the shipping class for which you want to hide the service. To Find out shipping_class_ids for your products, navigate to Products > Shipping Class in Admin Panel.
- You also need to find out the service code for the service which you want to hide. To Find out Service code, navigate to Cart/Checkout page. Right-click on the unwanted service and select Inspect. Check ‘value’ of shipping service to get the code for the service.
add_filter('woocommerce_package_rates', 'hide_shipping_method_when_shipping_class_product_is_in_cart', 10, 2);
function hide_shipping_method_when_shipping_class_product_is_in_cart($available_shipping_methods, $package)
{
// Shipping class IDs that need the method removed
$shipping_class_ids = array(
1111,
2232,
4235
);
$shipping_services_to_hide = array(
'wf_shipping_usps:D_PRIORITY_MAIL',
'wf_shipping_usps:D_EXPRESS_MAIL',
'wf_shipping_usps:D_MEDIA_MAIL'
);
$shipping_class_exists = false;
foreach(WC()->cart->cart_contents as $key => $values) {
if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) {
$shipping_class_exists = true;
break;
}
}
if ($shipping_class_exists) {
foreach($shipping_services_to_hide as & $value) {
unset($available_shipping_methods[$value]);
}
}
return $available_shipping_methods;
}
Code Snippet 02: Hide the undesired service for the required states
-
- Add the following code to your functions.php or anywhere relevant.
- Find the SERVICE_CODE from the FedEx shipping settings page and prepare the shipping service “wf_fedex_woocommerce_shipping: SERVICE_CODE”.
- Modify the shipping service and the list of states in the following code as per your requirement.
For example: To disable FedEx ground shipping service for a couple of states in the USA. $exclude = array( ‘wf_fedex_woocommerce_shipping:FEDEX_GROUND’ => array(‘AK’,’AZ’,’CA’,’CO’));
The code snippet is as given below:
add_filter('woocommerce_package_rates', 'wf_hide_undesired_service_for_required_states', 10, 2);
function wf_hide_undesired_service_for_required_states($rates, $package)
{
$exclude = array(
'international_delivery' => array(
'CA'
) ,
'wf_fedex_woocommerce_shipping:FEDEX_GROUND' => array(
'FL'
)
);
if (is_array($exclude)) {
foreach($exclude as $shipping_method => $excluded_states) {
if (in_array(WC()->customer->shipping_state, $excluded_states)) {
unset($rates[$shipping_method]);
}
}
}
return $rates;
}
Code Snippet 03: Hide the undesired service for the required Zip Codes (FedEx)
Steps to add Code Snippet
- Add the following code to your functions.php or any where relevant.
- You need to find out the service code for the service which you want to hide. To Find out Service code, navigate to Cart/Checkout page. Right click on the unwanted service and select inspect. Check ‘value’ of shipping service to get the code for the service.
- Unset the rates for required service code.
For example: To disable FedEx ground shipping service for the zip code 93999, the code is unset( $rates [‘wf_fedex_woocommerce_shipping:FEDEX_GROUND’] );
The Code is as given below:
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 10, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$excluded_zip_array = array(
'93999'
);
if (in_array($woocommerce->customer->get_shipping_postcode() , $excluded_zip_array)) {
unset($rates['wf_fedex_woocommerce_shipping:FEDEX_GROUND']);
}
return $rates;
}
Code Snippet 04 – Hide a shipping method for the particular product(s)
add_filter('woocommerce_package_rates', 'hide_shipping_method_if_particular_product_available_in_cart', 10, 2);
function hide_shipping_method_if_particular_product_available_in_cart($available_shipping_methods)
{
global $woocommerce;
// products_array should be filled with all the products ids
// for which shipping method (stamps) to be restricted.
$products_array = array(
101,
102,
103,
104
);
// You can find the shipping service codes by doing inspect element using
// developer tools of chrome. Code for each shipping service can be obtained by
// checking 'value' of shipping option.
$shipping_services_to_hide = array(
'wf_usps_stamps:US-FC',
'wf_usps_stamps:US-XM',
'wf_usps_stamps:US-MM',
'wf_usps_stamps:US-LM',
'wf_usps_stamps:US-PP',
'wf_usps_stamps:US-PS',
'wf_usps_stamps:US-CM',
'wf_usps_stamps:US-PM',
'wf_usps_stamps:US-EMI',
'wf_usps_stamps:US-PMI',
'wf_usps_stamps:US-FCI'
);
// Get all products from the cart.
$products = $woocommerce->cart->get_cart();
// Crawl through each items in the cart.
foreach($products as $key => $item) {
// If any product id from the array is present in the cart,
// unset all shipping method services part of shipping_services_to_hide array.
if (in_array($item['product_id'], $products_array)) {
foreach($shipping_services_to_hide as & $value) {
unset($available_shipping_methods[$value]);
}
break;
}
}
// return updated available_shipping_methods;
return $available_shipping_methods;
}
Code Snippet 05 – Hide the undesired method for the particular existing shipping class (Australia Post)
Steps to add Code Snippet
- Add the following code to your functions.php or anywhere relevant.
- You need to find out the service code for the service which you want to hide. To Find out Service code, navigate to Cart/Checkout page. Right-click on the unwanted service and select Inspect. Check ‘value’ of shipping service to get the code for the service.
- Australia Post methods will not be shown in the cart/checkout page if the product belongs to the $shipping_class_ids array.
The Code is as given below:
add_filter('woocommerce_package_rates', 'hide_auspost_method_when_shipping_class_product_is_in_cart', 10, 2);
function hide_auspost_method_when_shipping_class_product_is_in_cart($available_shipping_methods, $package)
{
$shipping_class_ids = array(
16
);
$shipping_services_to_hide = array(
'wf_australia_post:AUS_PARCEL_REGULAR',
'wf_australia_post:AUS_PARCEL_EXPRESS',
'wf_australia_post:AUS_PARCEL_COURIER',
'wf_australia_post:INTL_SERVICE_EPI',
'wf_australia_post:INTL_SERVICE_ECI_PLATINUM',
'wf_australia_post:INTL_SERVICE_ECI_M',
'wf_australia_post:INTL_SERVICE_RPI',
'wf_australia_post:INTL_SERVICE_PTI',
'wf_australia_post:INTL_SERVICE_AIR_MAIL',
'wf_australia_post:INTL_SERVICE_SEA_MAIL',
);
$shipping_class_exists = false;
foreach(WC()->cart->cart_contents as $key => $values) {
if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) {
$shipping_class_exists = true;
break;
}
}
if ($shipping_class_exists) {
foreach($shipping_services_to_hide as & $value) {
unset($available_shipping_methods[$value]);
}
}
return $available_shipping_methods;
}
Code Snippet 06 – Hide the undesired First Class Mail services on ELEX USPS Plugin.
Add the following code to your functions.php or any where relevant.
The Code is as given below:
// Exclude the services First-Class Mail Postcards, First-Class Mail Large Envelope and First-Class Mail Stamped Letter
add_filter('usps_disable_first_class_rate_first-class-mail-postcards', 'disable_first_class_rate_for_this_service',10,3);
add_filter('usps_disable_first_class_rate_first-class-mail-large-envelope', 'disable_first_class_rate_for_this_service',10,3);
add_filter('usps_disable_first_class_rate_first-class-mail-stamped-letter', 'disable_first_class_rate_for_this_service',10,3);
function disable_first_class_rate_for_this_service() {
return true;
}
// Include First-Class Mail Parcel ( Default Behaviour)
add_filter('usps_disable_first_class_rate_first-class-mail-parcel', 'disable_first_class_rate_for_parcel_service',10,3);
function disable_first_class_rate_for_parcel_service() {
return false;
}
Code Snippet 07 – Hide/show USPS services based on the quantity of items in the cart
- Showing only First Class Mail service if the cart contains upto 2 items.
- Showing only Standard Post (previously known as USPS Retail Ground) service if the cart contains more than 2 to 30 items.
- Not showing any USPS service if the cart contains more than 30 items.
Add the following code to your functions.php or any where relevant.
The Code is as given below:
add_filter( 'woocommerce_package_rates', 'wf_adjust_shipping_rate', 10, 2 );
function wf_adjust_shipping_rate( $available_shipping_methods, $packages ){
// Total quantity is total unit
$unit = 0;
$apply_final_filter = TRUE;
foreach ( $packages['contents'] as $item ) {
$unit+=$item['quantity'];
}
// if unit less than 3 , show D_FIRST_CLASS only
if( $unit < 3 ){
$shipping_methods = array();
if( !empty( $available_shipping_methods['wf_shipping_usps:D_FIRST_CLASS'] ) ){
$apply_final_filter = FALSE;
$shipping_methods['wf_shipping_usps:D_FIRST_CLASS'] = $available_shipping_methods['wf_shipping_usps:D_FIRST_CLASS'];
$available_shipping_methods = $shipping_methods;
}
}// if unit between 3 and 30 , show D_STANDARD_POST only
elseif( $unit > 2 && $unit < 31 ){
$shipping_methods = array();
if(!empty($available_shipping_methods['wf_shipping_usps:D_STANDARD_POST'])){
$apply_final_filter = FALSE;
$shipping_methods['wf_shipping_usps:D_STANDARD_POST'] = $available_shipping_methods['wf_shipping_usps:D_STANDARD_POST'];
$available_shipping_methods = $shipping_methods;
}
}
// if no above conditions met , remove all USPS service
if( $apply_final_filter ){
foreach ($available_shipping_methods as $index => $data) {
if (strpos($index, 'wf_shipping_usps') !== false) {
unset($available_shipping_methods[$index]);
}
}
}
return $available_shipping_methods;
}
In the above code, $available_shipping_methods array stores all the available USPS services for shipping. $packages array stores all the information about the shipment package. If the $unit is less than 3, then only D_FIRST_CLASS shipping service is returned. If the $unit is more than 2 and less than 31, then only D_STANDARD_POST shipping service is returned. If the $unit is more than 30, then $available_shipping_methods is unset so that no USPS service will be displayed.
Check out the shipping plugins offered by ELEX:
- ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label
- ELEX WooCommerce USPS Shipping Plugin with Print Label
- ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce
- ELEX EasyPost (FedEx, UPS & USPS) Shipping & Label Printing Plugin for WooCommerce
- ELEX WooCommerce Australia Post Shipping Plugin with Print Label & Tracking
Explore our blog section for more related articles.
You can also check out WooCommerce and WordPress plugins in ELEX.
