As a WordPress developer, you may come across scenarios where you need to customize the payment options available to customers based on their email address during the WooCommerce checkout process. In this tutorial, we’ll show you how to restrict payment methods for specific email addresses, allowing only a certain payment method, like “Pay by Card,” to be used when a particular email is entered.
This can be useful for various situations, such as:
- Offering specific payment methods to particular customers.
- Running special promotions or offers for selected email lists.
- Enforcing the use of a specific payment gateway for internal testing or specific user groups.
In this example, we will disable all payment methods except “Pay by Card” when the customer’s email is test@test.com
. We’ll accomplish this by using WooCommerce’s woocommerce_available_payment_gateways
filter in PHP.
Step-by-Step Guide to Restrict Payment Methods Based on Email
Step 1: Add Code to Theme’s functions.php
The code we’ll use needs to go in your theme’s functions.php
file or a custom plugin. This code will check the customer’s email during the checkout process and filter the payment methods accordingly.
Here’s the code snippet:
add_filter( 'woocommerce_available_payment_gateways', 'restrict_payment_gateways_for_specific_email' );
function restrict_payment_gateways_for_specific_email( $available_gateways ) {
// Get the customer's email from the checkout form
if ( is_checkout() ) {
$customer_email = isset($_POST['billing_email']) ? sanitize_email($_POST['billing_email']) : '';
// Check if the email is "test@test.com"
if ( $customer_email === 'test@test.com' ) {
// Allowed gateway ID (e.g., 'stripe' for Stripe card payments)
$allowed_gateway_id = 'stripe'; // Change this to your card gateway's ID
// Loop through all gateways and unset those that are not the card payment gateway
foreach ( $available_gateways as $gateway_id => $gateway ) {
if ( $gateway_id !== $allowed_gateway_id ) {
unset( $available_gateways[$gateway_id] );
}
}
}
}
return $available_gateways;
}
Step 2: Customize the Allowed Payment Gateway
In the code above, we are allowing only the “Pay by Card” method (e.g., Stripe) if the customer’s email is test@test.com
. You’ll need to modify the $allowed_gateway_id
variable to match your payment gateway’s ID.
For example:
- Stripe card payments usually have an ID like
'stripe'
. - PayPal might have an ID like
'paypal'
.
To find the ID of your payment gateway:
- Go to WooCommerce > Settings > Payments.
- Look at the URL when you edit your desired gateway. The ID will be part of the URL, like
/admin.php?page=wc-settings&tab=checkout§ion=stripe
.
You can also refer to the WooCommerce documentation or inspect the available gateways programmatically.
Step 3: Save and Test
Once the code is in place and you’ve set the correct gateway ID, you’re ready to test it out. Go to the checkout page of your WooCommerce store, enter the email test@test.com
, and see that only the specified payment gateway (e.g., Stripe) is available.
For all other email addresses, WooCommerce will display the usual range of available payment methods.
Why This Approach Works
The woocommerce_available_payment_gateways
filter is a powerful hook in WooCommerce that allows developers to control which payment methods are available on the checkout page. By hooking into this filter, we can dynamically adjust the payment gateways based on customer data—like their email address—before the options are displayed on the frontend.
Key Points in the Code:
- Customer Email Capture: The customer’s email is accessed via
$_POST['billing_email']
, ensuring we have the email the user entered during checkout. - Conditional Logic: We check if the email matches
test@test.com
. If so, we modify the payment methods by looping through all available gateways and unsetting any gateway that isn’t the one we want (in this case, Stripe). - Clean and Safe Code: The email is sanitized using
sanitize_email()
to prevent any malicious input from causing issues.
Practical Use Cases
Here are some situations where this snippet can be particularly useful:
- VIP or Test Users: Restricting payment methods for specific email addresses can be useful when dealing with internal test accounts or VIP customers who require special treatment.
- Email-based Promotions: If you’re running a promotion where users from a specific email list can only pay with a certain method, this method provides a seamless way to enforce that.
- Email-based Fraud Prevention: You can limit certain suspicious email addresses to use only secure payment methods.
Final Thoughts
This simple solution gives you control over WooCommerce payment methods based on the customer’s email. It’s easy to implement and can be adapted to many different use cases. With just a few lines of code, you can ensure that your customers only see the payment methods that make sense for them, improving both their experience and your ability to manage payments effectively.
If you need to extend this functionality further, you can also include additional conditions (such as billing country, order amount, or user roles) to fine-tune which payment methods are available.
FAQs:
1. How do I find the payment gateway IDs for my store? You can find the gateway IDs in WooCommerce > Settings > Payments. When you click to edit any gateway, the ID is part of the URL, or you can inspect it through WooCommerce’s internal hooks.
2. Can I use this code for multiple email addresses? Yes! You can modify the condition to check for multiple emails. For example, you can check if the email is in an array of emails:
$restricted_emails = array( 'test@test.com', 'sample@domain.com' );
if ( in_array( $customer_email, $restricted_emails ) ) {
// Restrict payment gateways
}
3. Will this impact the performance of my site? No, this approach runs a simple conditional check on the email address during checkout and is lightweight. It won’t have any noticeable impact on your site’s performance.
By following this tutorial, you’ll be able to customize WooCommerce payment methods for specific users or email addresses, offering more control and flexibility for your store. Happy coding!
Leave a Reply