Secondary offline payment method for Woocommerce

Last edited:
November 30, 2023
Reading time:
6 mins

/

Blog

/

WooCommerce

/

Secondary offline payment...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

WooCommerce, the popular WordPress plugin for e-commerce, offers a robust system for handling online payments. However, there are instances where store owners need to incorporate custom offline payment gateways to cater to specific needs or local requirements. In this guide, we’ll delve into creating a secondary custom offline payment gateway for WooCommerce, enabling you to expand your payment options beyond the default online methods.

Use Cases

1. Dedicated offline payment method “Pay at store”

  • Example: Currently, there is no direct payment method that refers to paying on store pickup.
  • Solution: Offering a secondary offline payment gateway for paying on store pickup makes sure your customers will not be confused on the checkout process.

2. Localized Payment Methods

  • Example: Your WooCommerce store caters to a specific geographic location where conventional offline payment methods are prevalent.
  • Solution: Implementing a custom offline payment gateway allows customers in that region to make purchases using familiar payment methods like bank transfers or cash on delivery.

3. Membership or Subscription Services

  • Example: You offer subscription-based services and want to provide customers the option to pay offline for their recurring charges.
  • Solution: A custom offline gateway allows you to manage subscription renewals for customers who prefer manual payment methods like wire transfers.

4. B2B Transactions

  • Example: Your WooCommerce store primarily serves business-to-business (B2B) customers who prefer offline transactions for invoicing and accounting purposes.
  • Solution: Implementing a custom offline payment gateway facilitates smoother B2B transactions, allowing businesses to make purchases on credit or through invoicing.

Adding Code

1. Creating the Gateway Class

To begin, you’ll need to add a new class that extends the WC_Payment_Gateway class. This class will serve as the foundation for your custom payment gateway. Open your child theme’s functions.php file or create a custom plugin.

// Ensure this file is called in your functions.php or included in your custom plugin
class WC_Custom_Offline_Gateway extends WC_Payment_Gateway {
// Constructor method for initializing your gateway
public function __construct() {
$this->id = 'custom_offline_gateway';
$this->icon = apply_filters('woocommerce_custom_offline_gateway_icon', '');
$this->method_title = __('Custom Offline Gateway', 'woocommerce');
$this->method_description = __('Accept payments offline using custom methods.', 'woocommerce');
$this->has_fields = false;
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}// Additional methods for setting up your gateway, handling payments, etc.
}

// Instantiate the class
function add_custom_offline_gateway_class($methods) {
$methods[] = 'WC_Custom_Offline_Gateway';
return $methods;
}

add_filter('woocommerce_payment_gateways', 'add_custom_offline_gateway_class');

2. Configuring Gateway Settings

Inside the WC_Custom_Offline_Gateway class, implement the init_form_fields method to define the settings for your custom gateway.

public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Custom Offline Gateway', 'woocommerce'),
'default' => 'yes',
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('Custom Offline Payment', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.', 'woocommerce'),
'default' => __('Pay offline using custom methods.', 'woocommerce'),
),
);
}

3. Handling Payments

Implement the necessary methods for handling payments in your custom gateway class. For example, you can override the process_payment method.

public function process_payment($order_id) {
$order = wc_get_order($order_id);// Mark the order as complete
$order->payment_complete();// Reduce stock levels
$order->reduce_order_stock();// Remove items from the cart
WC()->cart->empty_cart();

// Return the success redirect URL
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order),
);
}

Make sure to customize these methods based on your specific requirements and the offline payment workflow you want to implement.

Additional Considerations

1. Security

  • Always prioritize the security of your payment gateways. Implement secure coding practices, validate inputs, and consider consulting with a security expert.

2. Testing

  • Before deploying the custom payment gateway on your live site, thoroughly test it in a staging environment to identify and resolve any potential issues.

3. User Interface

  • Consider enhancing the user interface by adding informative messages and clear instructions during the checkout process.

4. Documentation

  • Document the usage and setup instructions for your custom payment gateway. This will be helpful for both site administrators and for future edits of your code.

Provide a secondary offline payment method for your WooCommerce customers

Creating a secondary custom offline payment gateway for WooCommerce provides flexibility and customization for merchants catering to diverse markets and unique payment preferences. By following the steps outlined in this guide, you can seamlessly integrate a tailored payment solution into your WooCommerce store, enhancing the overall user experience and expanding your business reach. Remember to stay informed about updates to WooCommerce and regularly test your custom gateway to ensure compatibility with the latest versions of the plugin.