How to Create WooCommerce Payment Gateway

Create WooCommerce Payment GatewayDo you want to create WooCommerce Payment Gateway that accepts your custom payment option? In this tutorial, I want to briefly show you how to Create Payment Gateway in WooCommerce to allow your customers to pay through this custom WooCommerce payment gateway.

It is possible to create your own custom payment gateway. There are 2 ways to achieve this. You can use either a plugin or custom code.

However, it is important to note that plugins will bloat your site. This is why we will use a custom code snippet.

We will discuss each step in detail, to allow newbies to use this method.

How to Create WooCommerce Payment Gateway

In this post, we will create a plugin to add a new payment gateway. This means that you need to have programming skills to do this.

We will extend a WooCommerce class. WooCommerce provides us with several core classes, such as payment gateway or email classes.

These classes can be extended to add your own functionality, which saves time on development. It also ensures your plugin works in a standard way.

The WC_Payment_Gateway Class

The WC_Payment_Gateway class extends this to give us structure and functions that will be specific to payment methods, such as the ability to get order totals or the thank you page URL.

We can benefit from the structure if we extend this class. This will handle some functions for us, including getting the title and description and displaying it on the checkout page.

It is important to note that all payment gateways in WooCommerce will begin by extending the WC_Payment_Gateway class.

Here are the steps you need to follow:

1. Check if WooCommerce is Active

Since we will be extending a WooCommerce class, we need to check if it is active:

[php] // Make sure WooCommerce is active

if ( ! in_array( ‘woocommerce/woocommerce.php’, apply_filters( ‘active_plugins’, get_option( ‘active_plugins’ ) ) ) ) return;
[/php]

2. Build Your Child Class

To do this, we first need to wrap this in an init function and hook it into plugins_loaded later than the default priority.

After we check if WooCommerce is active, we load our class after WooCommerce core. This makes it a secondary check against fatal errors.

It also ensures that the WC_Payment_Gateway class is available. o extend a class, you’ll follow the format:

class My_Child_Class extends The_Parent_Class { }

Here’s what this will look like in our plugin:

[php] /**

* Offline Payment Gateway

* @class WC_Gateway_Offline

* @extends WC_Payment_Gateway

* @version 1.0.0

* @package WooCommerce/Classes/Payment

* @author Njengah

*/

add_action( ‘plugins_loaded’, ‘wc_offline_gateway_init’, 11 );

function wc_offline_gateway_init() {

class WC_Gateway_Offline extends WC_Payment_Gateway {

// You plugin code starts here

} // end \WC_Gateway_Offline class

}
[/php]

3. Construct the Gateway

Since we have out class, we need to build our __construct() function. This ensures that the variables that we need to include in our class.

Here are the variables that are required:

  • $this->id
  • $this->icon
  • $this->has_fields = true or false (bool)
  • $this->method_title
  • $this->method_description

After the variables have been set, the constructor will need a few other functions.

The Form fields will be set in the init_form_fields() function. This function adds all of the settings fields such as enabling the gateway and adding a title.

[php] $this->init_form_fields();
$this->init_settings();
[/php]

4. Init the Form Fields

In this section, we will create an init_form_fields() function to set up the form fields for our payment gateway.

The function does nothing in the parent class, but it makes sure there are no fatal errors if you don’t override it.

However, we will give it some functionality in our child class.

The basic fields that we should include are enabled title and description.

[php] /**

* Initialize Gateway Settings Form Fields

*/

public function init_form_fields() {

$this->form_fields = apply_filters( ‘wc_offline_form_fields’, array(

‘enabled’ => array(

‘title’ => __( ‘Enable/Disable’, ‘wc-gateway-offline’ ),

‘type’ => ‘checkbox’,

‘label’ => __( ‘Enable Offline Payment’, ‘wc-gateway-offline’ ),

‘default’ => ‘yes’

),

‘title’ => array(

‘title’ => __( ‘Title’, ‘wc-gateway-offline’ ),

‘type’ => ‘text’,

‘description’ => __( ‘This controls the title for the payment method the customer sees during checkout.’, ‘wc-gateway-offline’ ),

‘default’ => __( ‘Offline Payment’, ‘wc-gateway-offline’ ),

‘desc_tip’ => true,

),

‘description’ => array(

‘title’ => __( ‘Description’, ‘wc-gateway-offline’ ),

‘type’ => ‘textarea’,

‘description’ => __( ‘Payment method description that the customer will see on your checkout.’, ‘wc-gateway-offline’ ),

‘default’ => __( ‘Please remit payment to Store Name upon pickup or delivery.’, ‘wc-gateway-offline’ ),

‘desc_tip’ => true,

),

‘instructions’ => array(

‘title’ => __( ‘Instructions’, ‘wc-gateway-offline’ ),

‘type’ => ‘textarea’,

‘description’ => __( ‘Instructions that will be added to the thank you page and emails.’, ‘wc-gateway-offline’ ),

‘default’ => ”,

‘desc_tip’ => true,

),

) );

}
[/php]

5. Process the Payment

This is the most important section when it comes to creating a payment gateway. We need to add a function to handle the processing of the order, telling WooCommerce what status is should have and where customers go after it’s used:

[php] public function process_payment( $order_id ) {

$order = wc_get_order( $order_id );

// Mark as on-hold (we’re awaiting the payment)

$order->update_status( ‘on-hold’, __( ‘Awaiting offline payment’, ‘wc-gateway-offline’ ) );

// Reduce stock levels

$order->reduce_order_stock();

// Remove cart

WC()->cart->empty_cart();

// Return thankyou redirect

return array(

‘result’ => ‘success’,

‘redirect’ => $this->get_return_url( $order )

);

}
[/php]

6. Add Payment Gateway Info to Order Received and Emails

The gateway we have added requires further instructions to complete the payment. We need to ensure that these instructions are shown on both the thank you page and order emails, using the thankyou_page() and email_instructions() stub methods.

[php] /**

* Output for the order received page.

*/

public function thankyou_page() {

if ( $this->instructions ) {

echo wpautop( wptexturize( $this->instructions ) );

}

}

/**

* Add content to the WC emails.

*

* @access public

* @param WC_Order $order

* @param bool $sent_to_admin

* @param bool $plain_text

*/

public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {

if ( $this->instructions && ! $sent_to_admin && ‘offline’ === $order->payment_method && $order->has_status( ‘on-hold’ ) ) {

echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;

}

}
[/php]

7. Add to WooCommerce Payment Gateways

The last step is to ensure that the payment gateway is available under WooCommerce > Settings > Checkout.

We will use the woocommerce_payment_gateways filter that gives us the array of all available gateways. This means that we will add our gateway into this array, and then return the array with our gateway added.

[php] function wc_offline_add_to_gateways( $gateways ) {

$gateways[] = ‘WC_Gateway_Offline’;

return $gateways;

}

add_filter( ‘woocommerce_payment_gateways’, ‘wc_offline_add_to_gateways’ );
[/php]

Conclusion

That all you need to do to add a custom payment gateway. We have simply cloned the functionality of the “Cheque” gateway.

If you’re integrating with a payment processor, it is important to note that you need to incorporate posting and receiving information from the payment processor.

If this process is too complex, you can hire a qualified developer. This will ensure that you do not break down your site.

Similar Articles

  1. WooCommerce Redirect After Logout [Ultimate Guide]
  2. How to Customize WooCommerce Product Pages
  3. How to Change Checkout Endpoints WooCommerce