What can we help you with?

How to hide or disable a WooCommerce shipping service? (Code Snippets)

In this article, we have provided code snippets to hide or disable WooCommerce shipping services based on different conditions.

Code Snippet 01: Hide a shipping method for a particular Shipping Class in USPS Plugin

With the below-given piece of code, you can hide an unwanted shipping method(service) for the product(in cart) of the particular shipping class.
Steps to add Code Snippet
  1. Add the following code to your functions.php or anywhere relevant.
  2. 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.
  3. 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.
With this, if any product(in cart) belongs to the listed shipping class in the code then the services in the $shipping_services_to_hide array will not be available in cart/checkout page.
The code snippet is as given below:
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

With this piece of code, you can hide an undesired service for the required states even though the service is enabled in FedEx plugin Settings.
Steps to add Code Snippet
    1. Add the following code to your functions.php or anywhere relevant.
    2. Find the SERVICE_CODE from the FedEx shipping settings page and prepare the shipping service “wf_fedex_woocommerce_shipping: SERVICE_CODE”.
    3. 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)

With this piece of code, you can hide an undesired service for the required Zip Codes even though the service is enabled in FedEx plugin Settings.

Steps to add Code Snippet

  1. Add the following code to your functions.php or any where relevant.
  2. 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.
  3. 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)

With the given piece of code, you can hide an unwanted shipping method for the particular product(s) which is in the cart.
Add the following code to your functions.php or anywhere relevant.
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)

With this piece of code, you can hide an undesired service for the required existing shipping class.

Steps to add Code Snippet

  1. Add the following code to your functions.php or anywhere relevant.
  2. 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.
  3. 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. 

IDs returned by USPS API for the First-Class Mail services such as First-Class Mail Postcards, First-Class Mail Large Envelope, First-Class Mail Stamped Letter and, First-Class Mail Parcel are same. ELEX USPS plugin displays the lowest rate among these First- Class Mail services. If that does not suit to your needs, you can disable the undesired First-Class Mail services.
With this piece of code, you can hide undesired First-Class Mail services and display only required First-Class Mail Service.
For Example: In the below code, First-Class Mail services such as First-Class Mail Postcards, First-Class Mail Large Envelope and First-Class Mail Stamped Letter are disabled. The rate of only First-Class Mail Parcel will be displayed to the customer.

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

With this piece of code, you can hide/show a required USPS service based on the number of items for the following cases:
  1. Showing only First Class Mail service if the cart contains  upto 2 items.
  2. Showing only  Standard Post (previously known as USPS Retail Ground) service if the cart contains more than 2 to 30 items.
  3. 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.

 


Explore our blog section for more related articles.

You can also check out WooCommerce and WordPress plugins in ELEX.

Previous Remove the (optional) Text from WooCommerce Checkout Fields
Next WooCommerce Shipping – Set Minimum Shipping Cost
You must be logged in to post a comment.