The code snippet provided below can be useful when you want to compare WooCommerce payment gateways and perform specific actions based on the selected payment method.
/**
* Compare WooCommerce Payment Gateways
*/
function wpsnippets_compare_payment_gateways( $available_gateways ) {
// Get the selected payment gateway
$selected_gateway = WC()->session->get( 'chosen_payment_method' );
// Perform actions based on the selected payment gateway
if ( 'paypal' === $selected_gateway ) {
// Do something for PayPal
} elseif ( 'stripe' === $selected_gateway ) {
// Do something for Stripe
} elseif ( 'bank_transfer' === $selected_gateway ) {
// Do something for Bank Transfer
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wpsnippets_compare_payment_gateways' );
This code snippet uses the woocommerce_available_payment_gateways
filter hook to compare the available payment gateways in WooCommerce. It retrieves the selected payment gateway from the WooCommerce session using WC()->session->get( 'chosen_payment_method' )
. Based on the selected payment gateway, you can perform specific actions or conditionally modify the available gateways.
In the example above, we compare the selected payment gateway with three options: PayPal, Stripe, and Bank Transfer. You can replace these with your desired payment gateways or add more conditions as needed. Inside each condition, you can perform custom actions or modifications specific to that payment gateway.
Remember to replace the placeholder actions (// Do something for PayPal
, etc.) with your actual code logic.
This code snippet allows you to customize the behavior of your WooCommerce store based on the selected payment gateway. For example, you can display additional fields or instructions specific to certain payment methods, apply discounts, or modify the available shipping options.
Examples
Example 1: Comparing WooCommerce Payment Gateways by Name
This example demonstrates how to compare WooCommerce payment gateways by their names. It retrieves all active payment gateways and compares their names to a specific value.
$gateways = WC_Payment_Gateways::instance()->get_available_payment_gateways();
$target_gateway_name = 'PayPal';
foreach ( $gateways as $gateway ) {
if ( $gateway->title === $target_gateway_name ) {
// Found the target gateway
// Perform actions specific to this gateway
break;
}
}
In this code example, we use the WC_Payment_Gateways::instance()->get_available_payment_gateways()
function to retrieve all active payment gateways. We then iterate through each gateway and compare its name ($gateway->title
) to the target gateway name ($target_gateway_name
). If a match is found, we can perform actions specific to that gateway.
Example 2: Comparing WooCommerce Payment Gateways by ID
This example demonstrates how to compare WooCommerce payment gateways by their IDs. It retrieves all active payment gateways and compares their IDs to a specific value.
$gateways = WC_Payment_Gateways::instance()->get_available_payment_gateways();
$target_gateway_id = 'paypal';
foreach ( $gateways as $gateway ) {
if ( $gateway->id === $target_gateway_id ) {
// Found the target gateway
// Perform actions specific to this gateway
break;
}
}
In this code example, we use the WC_Payment_Gateways::instance()->get_available_payment_gateways()
function to retrieve all active payment gateways. We then iterate through each gateway and compare its ID ($gateway->id
) to the target gateway ID ($target_gateway_id
). If a match is found, we can perform actions specific to that gateway.
Example 3: Comparing WooCommerce Payment Gateways by Support for Specific Features
This example demonstrates how to compare WooCommerce payment gateways based on their support for specific features. It retrieves all active payment gateways and checks if they support a specific feature.
$gateways = WC_Payment_Gateways::instance()->get_available_payment_gateways();
$target_feature = 'subscriptions';
foreach ( $gateways as $gateway ) {
if ( $gateway->supports( $target_feature ) ) {
// Gateway supports the target feature
// Perform actions specific to this gateway
}
}
In this code example, we use the WC_Payment_Gateways::instance()->get_available_payment_gateways()
function to retrieve all active payment gateways. We then iterate through each gateway and use the supports()
method to check if the gateway supports a specific feature ($target_feature
). If a gateway supports the feature, we can perform actions specific to that gateway.