How to Add Custom Payment Methods in WooCommerce Using ERP-Linked API Data

In a recent discussion in a Business Bloomer Club Slack thread, one member sought advice on customizing WooCommerce payment methods for an ERP-integrated store. Their ERP software provides a range of payment options via an API, and they wanted to extend WooCommerce’s available payment methods by adding user-related payment features specific to this ERP integration.

While WooCommerce offers standard payment options like BACS (Bank Transfer), COD (Cash on Delivery), and Cheque, the challenge was how to add a new method or customize an existing one to reflect the ERP’s payment configurations. Here’s a breakdown of the approaches discussed, from repurposing existing payment gateways to creating a fully custom gateway.

Using an Existing Payment Method (e.g., BACS) with Custom Descriptions

One suggestion is to repurpose an existing WooCommerce payment method, such as BACS, especially if it’s unused in the store. Here’s a basic approach to modify an existing method’s description using a snippet:

add_filter('woocommerce_gateway_description', 'custom_payment_gateway_description', 20, 2);
function custom_payment_gateway_description($description, $payment_id) {
    if ($payment_id == 'bacs') {
        $description = 'Custom description for BACS, tailored to ERP requirements.';
    }
    return $description;
}

This approach avoids adding an entirely new payment method, but it is limited if you need multiple ERP-specific payment options.

Creating a Custom WooCommerce Payment Gateway

For greater flexibility, creating a new custom payment gateway is a more robust solution. By using the woocommerce_payment_gateways filter, a developer can create a gateway that is specific to the ERP’s API requirements.

The steps below, based on Igor Benić’s guide, provide a starting point:

  1. Define the Payment Gateway Class
    Begin by creating a PHP class for the custom payment gateway. This class extends WooCommerce’s WC_Payment_Gateway class and sets properties like title, description, and instructions.
  2. Implement ERP-Specific Features
    Use API data from the ERP system to dynamically adjust settings. For instance, the ERP data could influence payment instructions or provide validation options specific to user-related metadata.
  3. Add the Gateway to WooCommerce
    Use the woocommerce_payment_gateways filter to register the new class as a payment gateway.

Here’s a basic structure for this approach:

add_filter('woocommerce_payment_gateways', 'add_custom_payment_gateway_class');
function add_custom_payment_gateway_class($gateways) {
    $gateways[] = 'WC_Gateway_Custom_Payment';
    return $gateways;
}

class WC_Gateway_Custom_Payment extends WC_Payment_Gateway {
    public function __construct() {
        $this->id = 'custom_payment';
        $this->method_title = 'ERP Custom Payment';
        $this->method_description = 'Custom payment gateway based on ERP data.';
        $this->init_form_fields();
        $this->init_settings();
        add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
    }
    // Define other methods as necessary
}

Conclusion

Adding custom payment methods in WooCommerce opens up possibilities for a tailored checkout experience. Whether by modifying an existing gateway or creating a new one from scratch, WooCommerce’s flexibility allows seamless integration of ERP-specific options, enhancing both customer experience and back-office compatibility. If you’re familiar with PHP, creating a custom gateway provides full control and scalability. Otherwise, repurposing an existing option can still yield great results.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

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