Home > WooCommerce > Hide WooCommerce Payment Gateway Based on Shipping Class

Hide WooCommerce Payment Gateway Based on Shipping Class

In this tutorial, we will learn how to hide the woocommerce payment gateway on the checkout page based on the shipping class. We will check for a specific product or for all product have specific shipping class. If a shipping class has been assigned to the product then hide the specific payment gateway on the checkout page.

Sometimes there is a payment method that we want to hide from the checkout page if the product has a specific shipping class (delivery method).

For example, you have a product that only accepts the pay-on-delivery method and want to hide the other online payment gateways then you will make conditions for that category’s product.

So we will learn about these conditions to hide the woocommerce payment gateway based on the shipping class. We will use the functions.php file to make a hook function to run this functionality.

Let’s get started!!!

As you know, you can apply different shipping classes to every product. So we will loop through all the products in the cart to check their shipping classes. We can use the get_shipping_class() and get_shipping_class_id() function to get the shipping classes of products.

WooCommerce Shipping Class

So let’s jump to the main point of this tutorial to hide the woocommerce payment gateway method based on the shipping class.

We have two scenarios in case to disable the payment gateways based on the shipping class. First, if we want to check any product in the cart has a specific shipping class. The second is that if all products have a specific shipping class.

One Product in Cart has Specific Class

In this scenario, we will check if in the cart at least have one product that has a specific shipping class. Then we will make a condition to run the hook to hide the specific payment gateway on the checkout page.

Before checking the shipping class of the product in the cart, make sure you have applied the shipping class to that product from the backend. To check it, open your product in edit mode and click on the woocommerce Shipping tab. See the following image.

Product Shipping Class

After checking, add that product to the cart and also add more products in the cart that has no shipping class which we want to check.

Now, we will make an action hook to execute the code and hide the payment gateway for a specific shipping class. So add the following code in your functions.php file.

Note: Use child theme’s functions.php for any customization. Check this tutorial on how to create a child theme in WordPress?

In the above code, first, we checked that the user is not an admin and then we loop through all the cart products and checked the shipping class of each one. If the shipping class found ‘ybc-class‘ then we unset the PayPal payment method and exit from the loop.

We disabled the payment method with payment gateway ID or Slug. To check the payment method ID or Slug, go to the woocommerce Settings menu and then click on the Payment tab. Here you will see all the available payment methods. Select the payment method by click on it and you will see in the URL, there is a query parameter ‘section‘ that is your payment method’s Slug or ID.

For checking the shipping class slug, see the following image of woocommerce settings options.

WooCommerce Shipping Class

All the Products in Cart have Shipping Class

In this scenario, we will check if all the products in the cart have the same shipping class. Then we will hide the payment gateway if the condition will be true otherwise not.

So let’s have a look into the following code snippet to hide the woocommerce payment gateway based on all products have the same shipping class.

In the above code, it will check the user is not admin then check if all products have the same shipping class. If the condition will be true, the specified payment method will be hidden.

Thus, you can make any condition and disable any specific payment gateway for a specific product, if it has in the cart.

Conclusion

So, you learned about the woocommerce shipping class and how you can hide payment gateway based on the shipping class.

You also learned, how to check and make conditions if the cart has at least one product with that specific class and hide the payment method.

If you have any questions about the tutorial please let me know in the comment section, I’ll help you with that. Please share with your friends, if you like it.

More WooCommerce Tutorials

How to hide the specific products from the shop and search page?
How to change price of specific product in cart?
How to change place order button text?
How to add text after or before product title?

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment