A payment gateway is a crucial part of a marketplace. It allows customers to purchase products and the admin to distribute the vendor earnings. In WordPress, Dokan is one of the popular multivendor solutions that provide two types of payment method support, which are adaptive and non-adaptive.
Adaptive payment methods distribute the vendor earnings and admin commission after placing an order. On the other hand, non-adaptive payment methods require manual withdrawal requests for the withdrawal of vendor earnings.
By default, Dokan provides three payment methods to vendors for withdrawals if any marketplace uses non-adaptive payment methods, and those are the followings: PayPal, Bank Transfer, and Skrill.
However, many marketplace owners cannot/rely on using adaptive payment methods; instead, they like to use a non-adaptive payment method, and the Dokan-provided payment methods for the non-adaptive case might not be enough for them. In this stage, they find it difficult to use the Dokan plugin.
As a result, I have come up with a solution, and in this article, I will demonstrate how to add a new payment method for withdrawal in the vendor dashboard so that marketplace owners can add their desired payment method for vendors to withdraw their earnings.
So, without further due, let’s jump into the code…
Note: The code can be added to the child theme’s functions.php file. If the child theme is not available, it can be added using the Code Snippets plugin.
Add a new withdrawal method to the admin setting
Vendors will not get a payment method until a withdrawal method is enabled from the WordPress Admin Dashboard > Dokan > Settings > Withdraw Options setting screen. So, an option needs to add to that setting.
/**
*
* Register New Withdraw Method
*
*/
function register_new_withdraw_method( $methods ){
$methods['custom'] = [
'title' => __( 'Custom Payment', 'dokan' ),
'callback' => 'dokan_withdraw_method_custom'
];
return $methods;
}
add_filter( 'dokan_withdraw_methods', 'register_new_withdraw_method', 99 );
Add Input Field to the Payment Method
Once an option is added to the admin setting and enabled from the proper setting screen, now, it needs to add the input field to allow vendors to insert proper data for withdrawals.
/**
*
* Method Callback Function
*
*/
function dokan_withdraw_method_custom( $store_settings ){
$value = isset( $store_settings['payment']['custom']['value'] ) ? esc_attr( $store_settings['payment']['custom']['value'] ) : ''; ?>
<div class="dokan-form-group">
<div class="dokan-w8">
<div class="dokan-input-group">
<span class="dokan-input-group-addon"><?php esc_html_e( 'E-mail', 'dokan-lite' ); ?></span>
<input value="<?php echo esc_attr( $value ); ?>" name="settings[custom][value]" class="dokan-form-control value" placeholder="you@domain.com" type="text">
</div>
</div>
</div>
<?php if ( dokan_is_seller_dashboard() ) : ?>
<div class="dokan-form-group">
<div class="dokan-w8">
<input name="dokan_update_payment_settings" type="hidden">
<button class="ajax_prev disconnect dokan_payment_disconnect_btn dokan-btn dokan-btn-danger <?php echo empty( $value ) ? 'dokan-hide' : ''; ?>" type="button" name="settings[custom][disconnect]">
<?php esc_attr_e( 'Disconnect', 'dokan-lite' ); ?>
</button>
</div>
</div>
<?php endif; ?>
<?php
}
Save the Input Field Value
The aforementioned code allows to insert of data for withdrawals, but the field value needs to save. So, add the code after the above code.
/**
*
* Save Withdraw Method
*
*/
function save_withdraw_method_wise( $store_id, $dokan_settings ) {
if ( isset( $_POST['settings']['custom']['value'] ) ) {
$dokan_settings['payment']['custom'] = array(
'value' => sanitize_text_field( $_POST['settings']['custom']['value'] ),
);
}
update_user_meta( $store_id, 'dokan_profile_settings', $dokan_settings );
}
add_action( 'dokan_store_profile_saved', 'save_withdraw_method_wise', 10, 2 );
Add Method to the Connected Payment Method List
To show a connected payment method in the Payment Method list of the Vendor Dashboard > Store > Payment screen, add the below code.
/**
*
* Add Custom Withdraw Method to the Payment Method List
*
*/
function add_custom_withdraw_in_payment_method_list( $required_fields, $payment_method_id ){
if( 'custom' == $payment_method_id ){
$required_fields = ['value'];
}
return $required_fields;
}
add_filter( 'dokan_payment_settings_required_fields', 'add_custom_withdraw_in_payment_method_list', 10, 2 );
Add Method to the Active Withdraw Method List
Dokan allows vendors to send a withdrawal request using an active withdrawal method. Therefore, to allow vendors to send a withdrawal request using the custom payment method, it needs to add to the Active Withdraw Method List, and the below code will do the job.
/**
*
* Add Custom Withdraw Method to the Active Withdraw Method List
*
*/
function custom_method_in_active_withdraw_method( $active_payment_methods, $vendor_id ) {
$store_info = dokan_get_store_info( $vendor_id );
if ( isset( $store_info['payment']['custom']['value'] ) && $store_info['payment']['custom']['value'] !== false ) {
$active_payment_methods[] = 'custom';
}
return $active_payment_methods;
}
add_filter( 'dokan_get_seller_active_withdraw_methods', 'custom_method_in_active_withdraw_method', 99, 2 );
Add Method to the Available Withdraw Method Section
This code will add the custom payment method to the Available Withdraw Method List, from where vendors can choose a default withdrawal method.
/**
*
* Include Method to Available Withdraw Method Section
*
*/
function include_method_in_withdraw_method_section( $methods ){
$methods[] = 'custom';
return $methods;
}
add_filter( 'dokan_withdraw_withdrawable_payment_methods', 'include_method_in_withdraw_method_section' );
Add details in Withdrawal Requests
Admin can send the vendor earnings to a certain account based on the withdrawal requests data. This part of the code will send the added custom payment method info of the vendor during a withdrawal request.
/**
*
* Add details to the Withdrawal Requests
*
*/
function vue_admin_withdraw(){
?>
<script>
var hooks;
function getCustomPaymentDetails( details, method, data ){
if ( data[method] !== undefined ) {
if ( 'custom' === method) {
details = data[method].value || '';
}
}
return details;
}
dokan.hooks.addFilter( 'dokan_get_payment_details', 'getCustomPaymentDetails', getCustomPaymentDetails, 33, 3 );
</script>
<?php
}
add_action( 'admin_print_footer_scripts', 'vue_admin_withdraw', 99 );
P.S. - This code can be used for both email address or phone number based payment method
Hi, I am trying to use this code. But the active methods does not appear in the payment list already configured by vendor.
https://drive.google.com/file/d/18LihOY247OP-B5h3_qT1Jl0n2xeFZrRJ/view?usp=sharing
The article has been updated. You can check the latest code to get the method in the list.
Tanjir Al Mamun, I realy apresiate and thank you for your effot, time and knowlage you share here.