To hide the payment method based on the product ID in WooCommerce on WordPress, you can use custom code. You’ll need to add this code to your theme’s functions.php
file or use a custom plugin.
Here’s an example code snippet that demonstrates how you can hide a specific payment method based on the product ID:
add_filter('woocommerce_available_payment_gateways', 'hide_payment_gateway_based_on_product');
function hide_payment_gateway_based_on_product($available_gateways) {
// Check if the current page is a product page
if (is_product()) {
global $product;
// Specify the product ID for which you want to hide the payment method
$product_id_to_hide = 123;
// Check if the current product matches the specified product ID
if ($product->get_id() == $product_id_to_hide) {
// Specify the payment gateway ID you want to hide
$gateway_to_hide = 'paypal'; // Change this to the actual ID of the payment gateway
// Remove the payment gateway from the available gateways
if (isset($available_gateways[$gateway_to_hide])) {
unset($available_gateways[$gateway_to_hide]);
}
}
}
return $available_gateways;
}
In this example:
- Replace
123
with the actual product ID for which you want to hide the payment method. - Replace
'paypal'
with the actual ID of the payment gateway you want to hide.
Please note that payment gateway IDs can vary depending on the specific gateways you have installed and activated on your WooCommerce site. You can find the gateway ID by inspecting the HTML source code of the payment method on the checkout page or by checking the WooCommerce settings.
Remember to test this code on a staging site before applying it to your live site to ensure it works as expected. Additionally, make sure to back up your site before making any changes to the code.
If you want to hide a payment method based on multiple product IDs, you can modify the code to check if the current product’s ID matches any of the specified IDs. Here’s an updated version of the code to handle multiple product IDs:
add_filter('woocommerce_available_payment_gateways', 'hide_payment_gateway_based_on_product');
function hide_payment_gateway_based_on_product($available_gateways) {
// Check if the current page is a product page
if (is_product()) {
global $product;
// Specify the product IDs for which you want to hide the payment method
$product_ids_to_hide = array(123, 456, 789); // Add your product IDs here
// Check if the current product ID is in the array of product IDs to hide
if (in_array($product->get_id(), $product_ids_to_hide)) {
// Specify the payment gateway ID you want to hide
$gateway_to_hide = 'paypal'; // Change this to the actual ID of the payment gateway
// Remove the payment gateway from the available gateways
if (isset($available_gateways[$gateway_to_hide])) {
unset($available_gateways[$gateway_to_hide]);
}
}
}
return $available_gateways;
}
In this example:
- Update the
$product_ids_to_hide
array with the product IDs for which you want to hide the payment method. - Replace
'paypal'
with the actual ID of the payment gateway you want to hide.
This code will check if the current product’s ID is in the array of product IDs to hide and then conditionally remove the specified payment gateway.
Remember to test this code thoroughly on a staging site before deploying it to your live site, and always have a backup of your site before making any code modifications.