Product Shipping Class Usage in PHP Code for WooCommerce

When you create a shipping class in WooCommerce, you are creating a custom taxonomy term.

Your custom taxonomy term is created for the product_shipping_class taxonomy, a custom taxonomy for the product custom post type.

Working example : Go to your WordPress Dashboard > WooCommerce > Settings > Shipping > classes > Add Shipping Class and click Add Shipping Class.

You have now created a custom taxonomy term named sample.

The taxonomy this term relates to is named product_shipping_class

You can now use your custom taxonomy term named sample in code like this :

has_term( 'sample', 'product_shipping_class', $product )

has_term is a WordPress function you can use with WooCommerce to check if a product is assigned a custom taxonomy term for a shipping class.

  1. The 1st parameter is your term named sample
  2. The 2nd parameter is the taxonomy named product_shipping_class
  3. The 3rd parameter is the product, if not set, defaults to the current product.

You can use this line of code to check if any product in the cart has the shipping class slug sample.

This code can then be used to target products by shipping class to customize and control shipping methods, rates and conditions using WooCommerce.

Here’s a full working code snippet which shows you how to use your custom taxonomy term to check if any product in the cart has the shipping class “sample”.

Related Code