-
Notifications
You must be signed in to change notification settings - Fork 1
/
woocommerce-add-extra-charges-option-to-payment-gateways.php
166 lines (150 loc) · 6.99 KB
/
woocommerce-add-extra-charges-option-to-payment-gateways.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/*
Plugin Name: WooCommerce Add Charges To Payment Gateway
Plugin URI: http://www.mrova.com
Description: You can add extra fee for any payment gateways
Version: 0.9
Author: Sunny Luthra
Author URI: http://www.mrova.com
*/
/**
* Copyright (c) `date "+%Y"` Sunny Luthra. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_PaymentGateway_Add_Charges{
public function __construct(){
$this -> current_gateway_title = '';
$this -> current_gateway_extra_charges = '';
add_action('admin_head', array($this, 'add_form_fields'));
add_action( 'woocommerce_calculate_totals', array( $this, 'calculate_totals' ), 10, 1 );
wp_enqueue_script( 'wc-add-extra-charges', $this->plugin_url() . '/assets/app.js', array('wc-checkout'), false, true );
}
function add_form_fields(){
global $woocommerce;
// Get current tab/section
$current_tab = ( empty( $_GET['tab'] ) ) ? '' : sanitize_text_field( urldecode( $_GET['tab'] ) );
$current_section = ( empty( $_REQUEST['section'] ) ) ? '' : sanitize_text_field( urldecode( $_REQUEST['section'] ) );
if($current_tab == 'checkout' && $current_section!=''){
$gateways = $woocommerce->payment_gateways->payment_gateways();
foreach($gateways as $gateway){
if(strtolower(get_class($gateway))==$current_section){
$current_gateway = $gateway -> id;
$extra_charges_id = 'woocommerce_'.$current_gateway.'_extra_charges';
$extra_charges_type = $extra_charges_id.'_type';
if(isset($_REQUEST['save'])){
update_option( $extra_charges_id, $_REQUEST[$extra_charges_id] );
update_option( $extra_charges_type, $_REQUEST[$extra_charges_type] );
}
$extra_charges = get_option( $extra_charges_id);
$extra_charges_type_value = get_option($extra_charges_type);
}
}
?>
<script>
jQuery(document).ready(function($){
$data = '<h4>Add Extra Charges</h4><table class="form-table">';
$data += '<tr valign="top">';
$data += '<th scope="row" class="titledesc">Extra Charges</th>';
$data += '<td class="forminp">';
$data += '<fieldset>';
$data += '<input style="" name="<?php echo $extra_charges_id?>" id="<?php echo $extra_charges_id?>" type="text" value="<?php echo $extra_charges?>"/>';
$data += '<br /></fieldset></td></tr>';
$data += '<tr valign="top">';
$data += '<th scope="row" class="titledesc">Extra Charges Type</th>';
$data += '<td class="forminp">';
$data += '<fieldset>';
$data += '<select name="<?php echo $extra_charges_type?>"><option <?php if($extra_charges_type_value=="add") echo "selected=selected"?> value="add">Total Add</option>';
$data += '<option <?php if($extra_charges_type_value=="percentage") echo "selected=selected"?> value="percentage">Total % Add</option>';
$data += '<br /></fieldset></td></tr></table>';
$('.form-table:last').after($data);
});
</script>
<?php
}
}
public function calculate_totals( $totals ) {
global $woocommerce;
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
$current_gateway = '';
if ( ! empty( $available_gateways ) ) {
// Chosen Method
if ( isset( $woocommerce->session->chosen_payment_method ) && isset( $available_gateways[ $woocommerce->session->chosen_payment_method ] ) ) {
$current_gateway = $available_gateways[ $woocommerce->session->chosen_payment_method ];
} elseif ( isset( $available_gateways[ get_option( 'woocommerce_default_gateway' ) ] ) ) {
$current_gateway = $available_gateways[ get_option( 'woocommerce_default_gateway' ) ];
} else {
$current_gateway = current( $available_gateways );
}
}
if($current_gateway!=''){
$current_gateway_id = $current_gateway -> id;
$extra_charges_id = 'woocommerce_'.$current_gateway_id.'_extra_charges';
$extra_charges_type = $extra_charges_id.'_type';
$extra_charges = (float)get_option( $extra_charges_id);
$extra_charges_type_value = get_option( $extra_charges_type);
if($extra_charges){
if($extra_charges_type_value=="percentage"){
$totals -> cart_contents_total = $totals -> cart_contents_total + ($totals -> cart_contents_total*$extra_charges)/100;
}else{
$totals -> cart_contents_total = $totals -> cart_contents_total + $extra_charges;
}
$this -> current_gateway_title = $current_gateway -> title;
$this -> current_gateway_extra_charges = $extra_charges;
$this -> current_gateway_extra_charges_type_value = $extra_charges_type_value;
add_action( 'woocommerce_review_order_before_order_total', array( $this, 'add_payment_gateway_extra_charges_row'));
}
}
return $totals;
}
function add_payment_gateway_extra_charges_row(){
?>
<tr class="payment-extra-charge">
<th><?php echo $this->current_gateway_title?> Extra Charges</th>
<td><?php if($this->current_gateway_extra_charges_type_value=="percentage"){
echo $this -> current_gateway_extra_charges.'%';
}else{
echo woocommerce_price($this -> current_gateway_extra_charges);
}?></td>
</tr>
<?php
}
/**
* Get the plugin url.
*
* @access public
* @return string
*/
public function plugin_url() {
if ( $this->plugin_url ) return $this->plugin_url;
return $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
*
* @access public
* @return string
*/
public function plugin_path() {
if ( $this->plugin_path ) return $this->plugin_path;
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
}
}
new Wc_PaymentGateway_Add_Charges();