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.

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 the above code we check shipping classes in every product in the cart and once we found a product with “no-paypal” shipping class (you have to put shipping class slug on line 16), we disable PayPal payment method an exit from the loop.
- On lines 17 and 18 you have to specify a payment gateway slug you would like to deactivate, how to find it you can read in this tutorial.
- If you don’t know where to add this code, you can use your current theme’s
functions.php
file but please be careful, if your theme receive updates, better use a child theme or a custom plugin for that. - If for some reason you decide to use
get_shipping_class_id()
method instead ofget_shipping_class()
, this tutorial probably will be helpful for you.
In case you’re not sure where to find a shipping class slug, here is the screenshot:

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
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