Hide Payment Methods Based on Shipping Class in the Cart

As you probably know, shipping classes apply to every product individually.

What does it mean for us? It means that we have to loop through all the products in the cart in our code and check their shipping classes using either get_shipping_class() or get_shipping_class_id() methods.

WooCommerce product shipping classes
In this example I created a new shipping class “No PayPal” (in WooCommerce > Settings > Shipping > Shipping Classes), so if there are products in the cart with this shipping class we are going to deactivate PayPal payment gateway.

Also we have two options here – if any product in the cart has a specific shipping class or if all the products in the cart have the specific shipping class.

Let’s go! ⚡️

If you need some custom coding related to WooCommerce payments, contact me.

At least One Product in Cart has Shipping Class

/**
 * @snippet       Deactivate Payment Method if Any Product has Shipping Class
 * @author        Misha Rudrastyh
 * @url           https://rudrastyh.com/woocommerce/payment-gateways-by-shipping-class.html
 */
add_filter( 'woocommerce_available_payment_gateways', 'rudr_hide_paypal_if_any_product_has_shipping_class' );

function rudr_hide_paypal_if_any_product_has_shipping_class( $gateways ) {

	// do nothing in /wp-admin
	if( is_admin() ) {
		return $gateways;
	}
	
	foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
		if ( 'no-paypal' === $values[ 'data' ]->get_shipping_class() ) {
			if( isset( $gateways[ 'paypal' ] ) ) {
				unset( $gateways[ 'paypal' ] );
				break;
			}
		}
	}

	return $gateways;
	
}

In case you’re not sure where to find a shipping class slug, here is the screenshot:

where to find WooCommerce shipping classes
In WooCommerce > Settings > Shipping > Shipping Classes you can find a shipping class slug or create a new shipping class.

All the Products in Cart have Shipping Class

/**
 * @snippet       Deactivate Payment Method if All Products have Shipping Class
 * @author        Misha Rudrastyh
 * @url           https://rudrastyh.com/woocommerce/payment-gateways-by-shipping-class.html
 */
add_filter( 'woocommerce_available_payment_gateways', 'rudr_hide_paypal_if_all_products_have_shipping_class' );

function rudr_hide_paypal_if_all_products_have_shipping_class( $gateways ) {

	// do nothing in /wp-admin
	if( is_admin() ) {
		return $gateways;
	}
	
	$has_shipping_class = true;

	foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
		if ( 'no-paypal' !== $values[ 'data' ]->get_shipping_class() ) {
			$has_shipping_class = false;
			break;
		}
	}

	if( $has_shipping_class ) {
		if( isset( $gateways[ 'paypal' ] ) ) {
			unset( $gateways[ 'paypal' ] );
		}
	}

	return $gateways;
	
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X