Blog / WooCommerce/ How to Customize WooCommerce Payment Gateway Titles and Descriptions

How to Customize WooCommerce Payment Gateway Titles and Descriptions

如何自定义WooCommerce支付网关的标题和描述?

Introduction

In a WooCommerce store, the payment gateway title and description are key pieces of information customers see during checkout. The default display may not align with your brand or business needs. This article details how to customize this text using code and filters, ensuring you use the official, recommended methods.

Core Method: Using WooCommerce Filters

WooCommerce provides a powerful filter system for modifying payment gateway display information. Two primary filters are used:

  • woocommerce_gateway_title: Modifies the payment gateway title.
  • woocommerce_gateway_description: Modifies the payment gateway description.

Basic Code Example

Add the following code to your child theme's functions.php file or use a code snippets plugin.

/**
 * Customize payment gateway title and description
 */
add_filter('woocommerce_gateway_title', 'custom_payment_gateway_title', 10, 2);
add_filter('woocommerce_gateway_description', 'custom_payment_gateway_description', 10, 2);

function custom_payment_gateway_title($title, $gateway_id) {
    // Conditional logic based on gateway ID
    if ($gateway_id === 'cod') {
        $title = 'Cash on Delivery';
    } elseif ($gateway_id === 'bacs') {
        $title = 'Bank Transfer';
    } elseif ($gateway_id === 'cheque') {
        $title = 'Check Payment';
    }
    // Add other gateways as needed
    return $title;
}

function custom_payment_gateway_description($description, $gateway_id) {
    // Customize description based on gateway ID
    if ($gateway_id === 'bacs') {
        $description = 'Please use the bank account details below for the transfer. Notify us via email or phone once completed.';
    } elseif ($gateway_id === 'cod') {
        $description = 'Pay with cash when your order is delivered.';
    }
    return $description;
}

Advanced Usage & Best Practices

1. Targeting Specific Gateways (e.g., PayPal)

For third-party gateways like PayPal Standard, the ID is typically paypal. Verify the ID in the gateway settings or documentation.

// Add condition for PayPal in the existing function
if ($gateway_id === 'paypal') {
    $title = 'PayPal Secure Checkout';
    // Or modify the description
    // $description = 'Complete your payment quickly and securely with PayPal.';
}

2. Conditional Dynamic Display

You can change text dynamically based on cart total, user role, or shipping zone.

function custom_payment_gateway_title_conditional($title, $gateway_id) {
    if ($gateway_id === 'cod' && WC()->cart->total > 500) {
        // Hide COD for orders over 500
        return 'Cash on Delivery not available for large orders';
    }
    return $title;
}
add_filter('woocommerce_gateway_title', 'custom_payment_gateway_title_conditional', 10, 2);

3. Adding Translation Support

For multilingual sites (using WPML or Polylang), ensure text is translatable.

if ($gateway_id === 'bacs') {
    $title = __('Bank Transfer', 'your-text-domain');
    $description = __('Please use the following bank account...', 'your-text-domain');
}

Important Notes

  • Safety First: Always back up your functions.php before editing. Use a child theme.
  • Gateway ID: Ensure the $gateway_id is correct. Find it in WooCommerce > Settings > Payments.
  • Filter Priority: The default priority is 10. Higher numbers run later. Usually, no change is needed.
  • Caching: Clear all caches (object, page) after making changes to see them immediately.

Conclusion

Using the official woocommerce_gateway_title and woocommerce_gateway_description filters allows you to safely and easily customize any payment gateway's display text. This method follows WooCommerce development standards and ensures best compatibility. Start with simple conditional replacements and add more complex logic as your business requires.

Post a Comment

Your email will not be published. Required fields are marked with *.