Build a Custom WooCommerce Payment Gateway Plugin.

Do you want to create your own WooCommerce payment gateway plugin? Then you’re are the right place. Just to let you know, this is part one of the series.

In this tutorial, I will show you how to create a custom WooCommerce payment gateway that you can hook in your kind of payment that is acceptable in your own country think of all the mobile payments options with public APIs.

The Woocommerce plugin allows you to choose the WooCommerce Payment Gateway that is available for you, place your order. However, in many countries that are available, there is an issue with how most of these payments are received.

Some countries don’t accept credit cards and others don’t have Paypal. These are actually some of the commonest ways of managing your payments as comes by default to Woocommerce.

We shall look at the basics and then you will be able to apply those and even make them better.

Now let’s jump into the code.

If you prefer video, watch this to become well acquainted with the process or continue reading the article.

Create the WooCommerce Payment Gateway Plugin

Step 1 – Setup the Plugin

  • Create a folder give it a name of your choice, I will call mine Noob WooCommerce Plugin
  • Create a php file inside the folder mine is noob-payment-for-woocommerce.php
  • Now add the plugin setup. 
  • Use the following code to setup your plugin

 * Plugin Name: Noob Payment for Woocommerce
 * Plugin URI:
 * Author Name: Laurence Bahiirwa
 * Author URI:
 * Description: This plugin allows for local content payment systems.
 * Version: 0.1.0
 * License: 0.1.0
 * License URL:
 * text-domain: noob-pay-woo

Save this and go to your plugin section and activate the plugin via the dashboard.

Step 2 – Check if WooCommerce is actively installed

First, you need to check if the woo-commerce plugin is actively installed. If you don’t have WooCommerce installed, install and activate it.

Then we activate our Noob Payment gateway plugin

Step3 -Start writing code

We will do an if statement and if that pases then we shall run some code. We shall check if not in Array because all our installed plugins are actually saved in Array.

We’re checking if woocommerce/woocommerce.php is not there, then apply some filters which is a WordPress function

It requires us to add a tag using the active_plugins method and the value is get_options.

Let’s check the active plugins available, and if these are not available, then we will not allow the plugin to process.

To check the woo-commerce plugin is active or not, add the following code snippet

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;

Build the WooCommerce Payment Gateway Plugin Class

Let’s start building our class for the payment Gateway plugin.

Before that, we have to check if some particular classes of WooCommerce actually exist.

Firstly, let’s add an action then hook into the Plugins loaded. If our plugins are loaded, then we shall look for a function in that function we shall initialize the noob payment. And then add a priority of 11.

Use the following code to add the gateway class.

add_action( 'plugins_loaded', 'noob_payment_init', 11 );

Extend the WooCommerece payment class

Now we can extend the WooCommerce payment class in our plugin to build a payment gateway.

We shall create a function and call it noob_payment_init then check for our class and instantiate the class. That means we shall create our new class WC_Noob_pay_Gateway which extends WC_Payment_Gateway

Use the following code to load the custom payment class.

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;
function noob_payment_init(){
    if( class_exists( 'WC_Payment_Gateway' ) ){
        class WC_Noob_pay_Gateway extends WC_Payment_Gateway{

As you can see in the above code we created the WC_Noob_pay_Gateway class and extend the WC_Payment_Gateway class.

Now your all code will be inside this class

Nothing is active yet but let’s test our plugin to see if it is still active.

Construct the Gateway Class

First, we shall instruct our Gateway class to have our construct method. We’re extending the classes and you realise we’re now working with Object-Oriented Programming.

We shall add a public function __construct() that is accessible by all and then we add our construct method. Since we’re extending the payment Gateway, some of the variables that we’re going to be accessing are already there.

Required properties for the payment gateway.

  • $this->id -id of the Gateway
  • this->icon -small logo on the checkout right at the bottom
  • this->has_fields -add fields inside our wooCommerce
  • $this->method_title – is translatable
  • $this->method_description -is also translatable
  • $this->init_form_fields()$this->init_form_fields() -this is where we shall describe our fields
  • $this->init_settings() -this will allow us initialize our settings

Meanwhile, you need to save this and refresh the plugin to check if anything is broken.

Our code now looks like this

if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) return;
function noob_payment_init(){
    if( class_exists( 'WC_Payment_Gateway' ) ){
        class WC_Noob_pay_Gateway extends WC_Payment_Gateway{
            public function __construct() {
                $this->id   = 'noob_payment';
                $this->icon = apply_filters( 'woocommerce_noob_icon', plugins_url('/assets/icon.png', __FILE__ ) );
                $this->has_fields = false;
                $this->method_title = __( 'Noob Payment', 'noob-pay-woo');
                $this->method_description = __( 'Noob local content payment systems.', 'noob-pay-woo');
                $this->title = $this->get_option( 'title' );
                $this->description = $this->get_option( 'description' );
                $this->instructions = $this->get_option( 'instructions', $this->description );

In the above code, we have defined the variables required for the Gateway and also defined the payment gateway load setting fields and then initialized the setting fields.

Initialize Form Fields

After that, we are now going to start adding content to our init form fields and those are the fields that we’re going to see when we come to add content to our form fields.

First, we are going to enable it, then we shall get the form fields class and apply filters. We sh have an array of things that we’re going to add.

We shall then check if it is enabled 

public function init_form_fields(){
            $this->form_fields = apply_filters( 'woo_noob_pay_fields', array(
                'enabled' => array(
                    'title' => __( 'Enable/Disable', 'noob-pay-woo'),

So when we save this and reload, we don’t see anything happening because we have not yet told WooCommerce that we actually have a new payment Gateway that is coming.

Tell WooCommerce about the Custom Payment Gateway Plugin

Let’s create a function which is a filter hook called woocommerce_payment_gateways and then we now add a function called add_to_woo_noob_payment_gateway 

Remember filters just edit, they don’t write any new content. It comes with the gateway’s argument, this argument this to be picked and returned.

We then get the gateways array and append a new item to it. we get our class that we extended that is WC_Noob_pay_Gateway and throw it here. 

add_filter( 'woocommerce_payment_gateways', 'add_to_woo_noob_payment_gateway');
function add_to_woo_noob_payment_gateway( $gateways ) {
    $gateways[] = 'WC_Noob_pay_Gateway';
    return $gateways;

When you save and reload that, you will actually see that we have our noob payment gateway coming into our woo commerce which is our new field.

See the following image of the listed custom woo-commerce payment method. And you will see all setting fields working and saving the values respectively.

WooCommerce payment gateway plugin

You can see the plugin setting fields inside this payment gateway, so click on it to see the fields and also check with input values to save.

You can see that we can actually enable it or disable it because of the enabled field that we added.

The next thing we’re going to do is add a proper title, description, and add instructions to the content area.

You can see the rest of the fields that we have added

Use the following code to initialize all the form fields.

public function init_form_fields() {
                $this->form_fields = apply_filters( 'woo_noob_pay_fields', array(
                    'enabled' => array(
                        'title' => __( 'Enable/Disable', 'noob-pay-woo'),
                        'type' => 'checkbox',
                        'label' => __( 'Enable or Disable Noob Payments', 'noob-pay-woo'),
                        'default' => 'no'
                    'title' => array(
                        'title' => __( 'Noob Payments Gateway', 'noob-pay-woo'),
                        'type' => 'text',
                        'default' => __( 'Noob Payments Gateway', 'noob-pay-woo'),
                        'desc_tip' => true,
                        'description' => __( 'Add a new title for the Noob Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo')
                    'description' => array(
                        'title' => __( 'Noob Payments Gateway Description', 'noob-pay-woo'),
                        'type' => 'textarea',
   'default' => __( 'Please remit your payment to the shop to allow for the delivery to be made', 'noob-pay-woo'),
                        'desc_tip' => true,
                        'description' => __( 'Add a new title for the Noob Payments Gateway that customers will see when they are in the checkout page.', 'noob-pay-woo')
                    'instructions' => array(
                        'title' => __( 'Instructions', 'noob-pay-woo'),
                        'type' => 'textarea',
                        'default' => __( 'Default instructions', 'noob-pay-woo'),
                        'desc_tip' => true,
                        'description' => __( 'Instructions that will be added to the thank you page and odrer email', 'noob-pay-woo')

The Process Payment Function

The next function that is very important in the woo commerce gateway is the process_payments function

We’re going to get the process_payments method and this is the one that actually deals with everything in terms of open-end.

We shall get the order id which is passed as an argument. In case we shall use wc_get_order

 $order = wc_get_order( $order_id );

Our process_payment method offers us the order id, we need this order id to get the order we’re going to pay for.

We now get the order status. Do this by appending a method which is update_status so we can immediately update the status of that order by awaiting noob payment when the order is pending.

 $order->update_status( 'on-hold',  __( 'Awaiting Noob Payment', 'noob-pay-woo') );

There are a number of statuses like processing, on hold, completed, cancelled, refunded and you can also create your own.

We need to check on our stock and reduce the order stock depending on how much you bought with that order for the next customer

We don’t want conflicts like someone saying I booked my order, paid for it yet I don’t have it and someone says sorry we ran out of stock because another client paid before you did.


Now we’re going to WooCommerce, get the general WooCommerce class and we go to the cart and empty the cart after that payment and then return a thank you Array.

This Array will have the result which is a success so when it is a success, then we have a message that we show and redirect the person to the class get_reurn_url and then pass in the order.

return array(
                    'result'   => 'success',
                    'redirect' => $this->get_return_url( $order ),

We have been able to add our own payment gateway by tapping into an API and adding a different function. Let’s add our own methods to allow us to do whatever we want and that’s how we create a Payment gateway.

Refresh the Noob payment gateway and try to make some orders.

Additionally, let’s save our user variables.

Update the WooCommerece Payment Gateway Plugin

Add an action that is going to take the hook that we’re tapping into which is woocommerce_update_options _payment_gateway. Then append to it the ID that we saved as noob payment.

add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
                // add_action( 'woocommerce_thank_you_' . $this->id, array( $this, 'thank_you_page' ) );

Add one more action that we allow us to say thank you and that is the woocommerce_thank_you.

It also takes the same ID which we are going to just append to our thank you.

We shall add an array to signify that we want to tap into a method which is a thankyou page

add_action( 'woocommerce_thank_you_' . $this->id, array( $this, 'thank_you_page' ) );

Now, let’s create this method

We shall use the if conditioner to check if the instructions are true, then we’re going to echo with wp_auto_paragraphs and we pass them in.

public function thank_you_page(){
                if( $this->instructions ){
                    echo wpautop( $this->instructions );

Make an order and checkout. You will notice that we now have Noob Payments Gateway in our payment options


We can now use this to pay for our goods and allow our customers to do so much more. I hope you’ve enjoyed this tutorial and if you have any questions please leave them in the comments.

Thank you for reading.

You can find all this code on Github.

Proceed to Part 2

Leave a Reply

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