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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
/* Copyright 2014 Varun Sridharan (email : varunsridharan23@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Plugin Name: Woocommerce Quick Donation
Plugin URI: http://varunsridharan.in/
Description: Woocommerce Quick Donation
Version: 0.1
Author: Varun Sridharan
Author URI: http://varunsridharan.in/
License: GPL2
*/
defined('ABSPATH') or die("No script kiddies please!");
define( 'wc_qd_u', plugin_dir_url( __FILE__ ) );
define( 'wc_qd_p', plugin_dir_path( __FILE__ ) );
class wc_quick_donation{
private $donation_id;
/**
* Setup The Plugin Class
*/
function __construct() {
add_action( 'admin_menu', array($this,'add_menu'));
add_filter( 'woocommerce_settings_tabs_array',array($this,'add_menu'), 50 );
add_action( 'woocommerce_settings_tabs_wc_quick_donation', array($this,'settings_page'));
add_action( 'woocommerce_update_options_wc_quick_donation', array($this,'update_settings') );
$this->donation_id = get_option('wc_quick_donation_product_id');
add_shortcode( 'wc_quick_donation', array($this,'shortcode_handler' ));
add_action('init',array($this,'process_donation'));
add_filter('woocommerce_get_price', array($this,'get_price'),10,2);
add_action('wc_qd_show_projects_list',array($this,'get_projects_list'));
add_action('woocommerce_checkout_update_order_meta', array($this,'add_order_meta'));
add_action( 'woocommerce_admin_order_data_after_billing_address', array($this,'my_custom_checkout_field_display_admin_order_meta'), 10, 1 );
}
/**
* Add a new settings tab to the WooCommerce settings tabs array.
*
* @param array $settings_tabs Array of WooCommerce setting tabs & their labels, excluding the Subscription tab.
* @return array $settings_tabs Array of WooCommerce setting tabs & their labels, including the Subscription tab.
*/
public function add_menu($settings_tabs){
$settings_tabs['wc_quick_donation'] = 'WC Quick Donation';
return $settings_tabs;
}
/**
* Uses the WooCommerce admin fields API to output settings via the @see woocommerce_admin_fields() function.
*
* @uses woocommerce_admin_fields()
* @uses self::get_settings()
*/
public static function settings_page() {
woocommerce_admin_fields( self::get_settings() );
}
public function add_order_meta( $order_id ) {
global $woocommerce;
update_post_meta( $order_id, 'project_details',$woocommerce->session->projects);
update_post_meta( $order_id, 'is_donation','yes');
$order = new WC_Order($order_id);
$format = sprintf(get_option('wc_quick_donation_order_notes_title'), $woocommerce->session->projects);
$order->add_order_note($format);
unset($order);
}
public function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.get_option('wc_quick_donation_project_section_title').' :</strong>'.get_post_meta( $order->id, 'project_details', true ) . '</p>';
}
/**
* Get all the settings for this plugin for @see woocommerce_admin_fields() function.
*
* @return array Array of settings for @see woocommerce_admin_fields() function.
*/
public static function get_settings() {
$settings = array(
'section_title' => array(
'name' => 'Woocommerce Quick Donation Settings',
'type' => 'title',
'desc' => 'Just Call <code><strong>[wc_quick_donation]</strong></code> short code any where in your page,post,widgets or template <br/>
To Customize the <strong>Donation Form</strong> copy the template file from <code>woocommerce-quick-donation/template/donation_form.php</code> to your <code>theme/woocommerce</code> folder.
',
'id' => 'wc_quick_donation_section_title'
),
'donate_redirect' => array(
'name' => 'Donation Redirect',
'desc' => 'Redirect User Checkout If Added To Cart.',
'id' => 'wc_quick_donation_redirect',
'type' => 'select',
'class' =>'chosen_select',
'options' => array('false' => 'No Redirect','true'=>'Redirect To Checkout Page')
),
'donate_redirect' => array(
'name' => 'Remove Cart Items',
'desc' => 'Removes Other Cart Items If Donation Aded To Cart.',
'id' => 'wc_quick_donation_cart_remove',
'type' => 'select',
'class' =>'chosen_select',
'options' => array('false' => 'Keep All Items','true'=>'Remove All Items')
),
'project_names' => array(
'name' => 'Project Names',
'type' => 'textarea',
'desc' => 'Add Names By <code>,</code> Seperated ',
'id' => 'wc_quick_donation_projects',
'default' => 'Project1,Project2'
),
'order_project_title' => array(
'name' => 'Order Project Title',
'type' => 'text',
'desc' => 'Title to view in order edit page',
'id' => 'wc_quick_donation_project_section_title',
'default' =>'For Project'
),
'order_notes_title' => array(
'name' => 'Order Notes Title',
'type' => 'text',
'desc' => 'to display project name use <code>Project Name : %s</code>',
'id' => 'wc_quick_donation_order_notes_title',
'default' =>'Project Name %s'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_tab_demo_section_end'
)
);
return apply_filters( 'wc_settings_tab_demo_settings', $settings );
}
/**
* Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function.
*
* @uses woocommerce_update_options()
* @uses self::get_settings()
*/
public static function update_settings() {
woocommerce_update_options(self::get_settings());
}
/**
* Check's For Donation Product Exist In Cart
* @returns Boolean True|False
*/
public function donation_exsits(){
global $woocommerce;
if( sizeof($woocommerce->cart->get_cart()) > 0){
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values){
$_product = $values['data'];
if($_product->id == $this->donation_id){
return true;
}
}
}
return false;
}
/**
* Gets Donation Current Price
* @param $price
* @param $product
* @returns 0 | price
*/
public function get_price($price, $product){
global $woocommerce;
if($product->id == $this->donation_id){
return isset($woocommerce->session->jc_donation) ? floatval($woocommerce->session->jc_donation) : 0;
}
return $price;
}
/**
* Process The Given Donation
*/
public function process_donation(){
global $woocommerce;
$donation = isset($_POST['jc-donation']) && !empty($_POST['jc-donation']) ? floatval($_POST['jc-donation']) : false;
$projects = isset($_POST['projects']) && !empty($_POST['projects']) ? $_POST['projects'] : false;
$_SESSION['wc_qd_projects'] = $projects;
if($donation && isset($_POST['donate-btn'])){
$found = false;
if($donation >= 0){
$woocommerce->session->jc_donation = $donation;
$woocommerce->session->projects = $projects;
if( sizeof($woocommerce->cart->get_cart()) > 0){
foreach($woocommerce->cart->get_cart() as $cart_item_key=>$values){
$_product = $values['data'];
if($_product->id == $this->donation_id)
$found = true;
}
if(!$found)
$this->remove_cart_items();
$woocommerce->cart->add_to_cart($this->donation_id);
$this->donation_redirect();
}else{
$this->remove_cart_items();
$woocommerce->cart->add_to_cart($this->donation_id);
$this->donation_redirect();
}
}
}
}
private function remove_cart_items(){
$cart_remove = get_option('wc_quick_donation_cart_remove');
if(isset($cart_remove) && $cart_remove == 'true'){
global $woocommerce;
$woocommerce->cart->empty_cart();
return true;
}
return true;
}
public function donation_redirect(){
global $woocommerce;
$redirect = get_option('wc_quick_donation_redirect');
if(isset($redirect) && $redirect == 'true'){
wp_safe_redirect($woocommerce->cart->get_checkout_url() );
exit;
}
return '';
}
public function wc_qd_form(){
global $woocommerce;
$donate = isset($woocommerce->session->jc_donation) ? floatval($woocommerce->session->jc_donation) : 0;
if(!$this->donation_exsits()){
unset($woocommerce->session->jc_donation);
unset($woocommerce->session->projects);
}
// uncomment the next line of code if you wish to round up the order total with the donation e.g. £53 = £7 donation
// $donate = jc_round_donation($woocommerce->cart->total );
if(!$this->donation_exsits()){
$wc_get_template = function_exists('wc_get_template') ? 'wc_get_template' : 'woocommerce_get_template';
$wc_get_template( 'donation_form.php', array(), '', wc_qd_p . 'template/' );
}
}
#$before,$after,$select_class,$select_id
public function get_projects_list(){
$projects_db = get_option('wc_quick_donation_projects');
if($projects_db){
$project = explode(',',$projects_db);
$project_list = '';
$project_list .= '<option value="" > Select A Project </option>';
foreach($project as $proj){
$project_list .= '<option value="'.$proj.'" > '.$proj.'</option>';
}
echo '<select name="projects">'.$project_list.'</select>';
}
}
/**
* wc_quick_donation shortcode Handler
*/
public function shortcode_handler(){
echo $this->wc_qd_form();
}
/**
* Install The Plugin
*/
public function install() {
$exist = get_option('wc_quick_donation_product_id');
if($exist){
return true;
} else {
$userID = 1;
if(get_current_user_id()){
$userID = get_current_user_id();
}
$post = array(
'post_author' => $userID,
'post_content' => 'Used For Donation',
'post_status' => 'publish',
'post_title' => 'Donation',
'post_type' => 'product',
);
$post_id = wp_insert_post( $post, $wp_error );
update_post_meta($post_id, '_stock_status', 'instock');
update_post_meta($post_id, '_tax_status', 'none');
update_post_meta($post_id, '_tax_class', 'zero-rate');
update_post_meta($post_id, '_visibility', 'hidden');
update_post_meta($post_id, '_stock', '');
update_post_meta($post_id, '_virtual', 'yes');
update_post_meta($post_id, '_featured', 'no');
update_post_meta($post_id, '_manage_stock', "no" );
update_post_meta($post_id, '_sold_individually', "yes" );
update_post_meta($post_id, '_sku', 'checkout-donation');
add_option('wc_quick_donation_product_id',$post_id);
add_site_option( 'wc_quick_donation_product_id', $post_id) ;
}
}
}
/**
* Check if WooCommerce is active
* if yes then call the class
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
register_activation_hook( __FILE__, array( 'wc_quick_donation', 'install' ) );
$wc_quick_buy = new wc_quick_donation;
}
?>