The code snippets in this tutorial shows you how to hide payment methods on your checkout page using WooCommerce.
Common Examples
Payment Options
- PayPal: ‘paypal’
- Credit Card: ‘stripe’ or ‘authorize_net’
- Cash on Delivery: ‘cod’
- Bank Transfer: ‘bacs’
- Check Payments: ‘cheque’
Other
- Hide Multiple Payment Options
- Hide Based on Product Price
- Hide Based on Shipping Method
- Hide Based on Customer Location
- Hide Based on Cart Contents
- Hide Based on Combined Conditions
Add the Code to Your Child Theme
- Open your child theme’s functions.php file
- Add this code at the very end of the file:
add_filter('woocommerce_available_payment_gateways', function($gateways) {
foreach ($gateways as $gateway_id => $gateway) {
if (!my_gateway_is_visible($gateway_id)) {
unset($gateways[$gateway_id]);
}
}
return $gateways;
});
Create the Visibility Function
- Add this function to your functions.php file:
function my_gateway_is_visible($gateway_id) {
// Example: Hide PayPal for specific products
if ($gateway_id === 'paypal') {
// Check if cart contains specific product
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
// Replace 123 with your product ID
if ($product_id === 123) {
return false;
}
}
}
return true;
}
Common Examples:
Hide for Specific Product Categories:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
if (has_term('downloadable', 'product_cat', $product_id)) {
return false;
}
}
}
return true;
}
Hide Based on Cart Total:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'cod') {
if (WC()->cart->total < 50) {
return false;
}
}
return true;
}
Hide for Specific User Roles:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
if (current_user_can('wholesale_customer')) {
return false;
}
}
return true;
}
Code Examples for Hiding Specific Payment Options:
Hide PayPal:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
return false; // Always hide PayPal
}
return true;
}
Hide Stripe:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'stripe') {
return false; // Always hide Stripe
}
return true;
}
Hide Cash on Delivery (COD):
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'cod') {
return false; // Always hide Cash on Delivery
}
return true;
}
Hide Bank Transfer:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'bacs') {
return false; // Always hide Bank Transfer
}
return true;
}
Hide Check Payments:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'cheque') {
return false; // Always hide Check Payments
}
return true;
}
Hide Multiple Payment Options:
function my_gateway_is_visible($gateway_id) {
// Array of payment methods to hide
$hidden_methods = array('paypal', 'stripe', 'cod');
if (in_array($gateway_id, $hidden_methods)) {
return false;
}
return true;
}
Hide Based on Product Price:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
foreach (WC()->cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
if ($product->get_price() > 100) {
return false; // Hide PayPal for products over $100
}
}
}
return true;
}
Hide Based on Shipping Method:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'cod') {
$chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
if (in_array('flat_rate:1', $chosen_shipping_methods)) {
return false; // Hide COD for specific shipping method
}
}
return true;
}
Hide Based on Customer Location:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
$customer_country = WC()->customer->get_billing_country();
if ($customer_country === 'US') {
return false; // Hide PayPal for US customers
}
}
return true;
}
Hide Based on Cart Contents:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'stripe') {
foreach (WC()->cart->get_cart() as $cart_item) {
$product = $cart_item['data'];
if ($product->is_virtual()) {
return false; // Hide Stripe for virtual products
}
}
}
return true;
}
Remember: You can combine these conditions to create more complex rules. For example:
function my_gateway_is_visible($gateway_id) {
if ($gateway_id === 'paypal') {
// Hide PayPal for US customers with orders over $100
if (WC()->customer->get_billing_country() === 'US' && WC()->cart->total > 100) {
return false;
}
}
return true;
}
Related Plugins :

Leave a Reply
You must be logged in to post a comment.