forked from vcatalasan/pmpro-products
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmpro-products.php
393 lines (336 loc) · 15.4 KB
/
pmpro-products.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
Plugin Name: PMPro Products
Plugin URI: http://www.bscmanage.com/my-plugin/
Description: Extends Paid Membership Pro to allow other non-subscription based products available to members on checkout
Version: 0.1.0
License: MPL
Author: Val Catalasan
*/
class PMPro_Products
{
private static $instance = null;
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance()
{
// If the single instance hasn't been set, set it now.
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
function __construct()
{
global $table_prefix, $wpdb;
$wpdb->pmpro_membership_orders_meta = $table_prefix . 'pmpro_membership_orders_meta';
add_action("pmpro_membership_level_after_other_settings", array($this, "pmpro_membership_level_after_other_settings"));
add_action("pmpro_save_membership_level", array($this, "pmpro_save_donation_option"));
add_filter("pmpro_level_cost_text", array($this, "pmpro_level_cost_text"), 10, 2);
add_action('pmpro_checkout_after_level_cost', array($this, 'pmpro_checkout_after_level_cost'));
add_filter("pmpro_checkout_level", array($this, "pmpro_checkout_level"));
add_filter("pmpro_registration_checks", array($this, "pmpro_registration_checks"));
add_filter('pmpro_checkout_order', array($this, 'pmpro_checkout_order'));
add_action('pmpro_added_order', array($this, 'pmpro_added_order'));
add_action('pmpro_invoice_bullets_bottom', array($this, 'pmpro_invoice_bullets_bottom'));
}
/*
Min Amount and Max Amount Fields on the edit levels page
*/
//fields on edit page
function pmpro_membership_level_after_other_settings()
{
global $pmpro_currency_symbol;
$level_id = intval($_REQUEST['edit']);
$fields = self::pmpro_get_donation_option($level_id);
$donation = $fields['donation'];
$min_amount = $fields['min_amount'];
$max_amount = $fields['max_amount'];
?>
<h3 class="topborder">Donation</h3>
<p>If donation is enabled, the donation amount will be added to the billing amount values you set on this level.</p>
<table>
<tbody class="form-table">
<tr>
<th scope="row" valign="top"><label for="level_cost_text">Enable Donation:</label></th>
<td>
<input type="checkbox" name="donation[new]" value="1" <?php checked($donation['new'], "1");?> /> New Membership<br/>
<input type="checkbox" name="donation[renewal]" value="1" <?php checked($donation['renewal'], "1");?> /> Membership Renewal
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="level_cost_text">Min Amount:</label></th>
<td>
<?php echo $pmpro_currency_symbol?><input type="text" name="min_amount"
value="<?php echo esc_attr($min_amount); ?>"/>
</td>
</tr>
<tr>
<th scope="row" valign="top"><label for="level_cost_text">Max Amount:</label></th>
<td>
<?php echo $pmpro_currency_symbol?><input type="text" name="max_amount"
value="<?php echo esc_attr($max_amount); ?>"/>
</td>
</tr>
</tbody>
</table>
<?php
}
//save level cost text when the level is saved/added
function pmpro_save_donation_option($level_id)
{
$donation = (array) $_REQUEST['donation'];
$min_amount = preg_replace("[^0-9\.]", "", $_REQUEST['min_amount']);
$max_amount = preg_replace("[^0-9\.]", "", $_REQUEST['max_amount']);
update_option("pmpro_donation_${level_id}", array('donation' => $donation, 'min_amount' => $min_amount, 'max_amount' => $max_amount));
}
function pmpro_get_donation_option($level_id)
{
$fields = get_option("pmpro_donation_${level_id}", array('donation' => array(), 'min_amount' => '', 'max_amount' => ''));
return $fields;
}
/*
Show form at checkout.
*/
//override level cost text on checkout page
function pmpro_level_cost_text($text, $level)
{
global $pmpro_pages;
if (is_page($pmpro_pages['checkout'])) {
$fields = self::pmpro_get_donation_option($level->id);
if (!empty($fields['donation'])) {
$text = "";
}
}
return $text;
}
//show form
function pmpro_checkout_after_level_cost()
{
global $current_user, $pmpro_currency_symbol, $pmpro_level, $gateway, $pmpro_msgt;
$membership_amount = $pmpro_level->initial_payment;
//get variable pricing info
$fields = self::pmpro_get_donation_option($pmpro_level->id);
$donation = $fields['donation'];
//no donation? just return
if (empty($donation))
return;
//check if donation is enabled on new membership or renewal
$new_membership = $this->is_new_membership($current_user->ID);
$enable = $new_membership && $donation['new'] ? true : (!$new_membership && $donation['renewal'] ? true : false);
//not enable, just return
if (!$enable)
return;
//okay, now we're showing the form
$min_amount = $fields['min_amount'];
$max_amount = $fields['max_amount'];
$max_amount < $min_amount and $max_amount = $min_amount;
$variable_amount = $min_amount != $max_amount;
/*
if (isset($_REQUEST['donation_amount']) && $pmpro_msgt != 'pmpro_error')
$amount= preg_replace("[^0-9\.]", "", $_REQUEST['donation_amount']);
else
*/
$amount = $min_amount;
?>
<div class="product-addon">
<p><input type="checkbox" id="donation-optin" checked="checked" /> I would like to make a one time <strong><?php echo $pmpro_currency_symbol . $min_amount ?></strong> donation
<?php if ($variable_amount) {
$add_amount = $max_amount - $min_amount;
$amount += $add_amount; ?>
plus an additional <?php echo $pmpro_currency_symbol ?> <input type="text" id="add-amount" size="10" value="<?php echo $add_amount;?>" />
<?php } ?>
<input type="hidden" id="donation-amount" name="donation_amount" size="10" value="<?php echo $amount;?>" /></p>
<p>Total Amount: <strong><?php echo $pmpro_currency_symbol ?><span id="total-amount"><?php echo $membership_amount + $amount ?></span></strong></p>
</div>
<script>
//some vars for keeping track of whether or not we show billing
var pmpro_gateway_billing = <?php if(in_array($gateway, array("paypalexpress", "twocheckout")) !== false) echo "false"; else echo "true";?>;
var pmpro_pricing_billing = <?php if(!pmpro_isLevelFree($pmpro_level)) echo "true"; else echo "false";?>;
//this script will hide show billing fields based on the price set
jQuery(document).ready(function ($) {
//bind check to price field
var pmprovp_price_timer;
$('#add-amount').bind('keyup change', function() {
var add_amount = isNaN(parseFloat(this.value)) ? 0 : parseFloat(this.value);
donation_amount = <?php echo $min_amount ?> + add_amount;
$('#donation-amount').attr('value', donation_amount);
pmprovp_price_timer = setTimeout(pmprovp_checkForFree, 500);
});
$('#donation-optin').click(function() {
this.checked ? $('#donation-amount').attr('value', <?php echo $amount ?>) : $('#donation-amount').attr('value', 0);
pmprovp_price_timer = setTimeout(pmprovp_checkForFree, 500);
});
if ($('input[name=gateway]')) {
$('input[name=gateway]').bind('click', function () {
pmprovp_price_timer = setTimeout(pmprovp_checkForFree, 500);
});
}
function update_total() {
var membership_amount = <?php echo $membership_amount ?>;
var donation_amount = parseFloat($('#donation-amount').val());
$('#total-amount').html( membership_amount + donation_amount );
}
function pmprovp_checkForFree() {
update_total();
var amount = parseFloat($('#total-amount').html());
//does the gateway require billing?
if ($('input[name=gateway]').length) {
var no_billing_gateways = ['paypalexpress', 'twocheckout'];
var gateway = $('input[name=gateway]:checked').val();
if (no_billing_gateways.indexOf(gateway) > -1)
pmpro_gateway_billing = false;
else
pmpro_gateway_billing = true;
}
//is there a donation amount?
if (amount)
pmpro_pricing_billing = true;
else
pmpro_pricing_billing = false;
//figure out if we should show the billing fields
if (pmpro_gateway_billing && pmpro_pricing_billing) {
$('#pmpro_billing_address_fields').show();
$('#pmpro_payment_information_fields').show();
pmpro_require_billing = true;
}
else {
$('#pmpro_billing_address_fields').hide();
$('#pmpro_payment_information_fields').hide();
pmpro_require_billing = false;
}
}
//check when page loads too
pmprovp_checkForFree();
});
</script>
<?php
}
//set amount
function pmpro_checkout_level($level)
{
if (isset($_REQUEST['donation_amount']))
$amount = preg_replace("[^0-9\.]", "", $_REQUEST['donation_amount']);
// check if a donation amount is passed and amount is valid then add to cart
if ($amount && $this->pmpro_registration_checks(true)) {
$level->cart['donation'] = number_format((float)$amount, 2, '.', '');
}
return $level;
}
//check price is between min and max
function pmpro_registration_checks($continue)
{
//only bother if we are continuing already
if ($continue) {
global $pmpro_currency_symbol, $pmpro_msg, $pmpro_msgt;
$donation_amount = $_REQUEST['donation_amount'];
//was an amount passed in?
if ($donation_amount) {
//get values
$level_id = intval($_REQUEST['level']);
$fields = self::pmpro_get_donation_option($level_id);
$donation = $fields['donation'];
$min_amount = $fields['min_amount'];
$max_amount = $fields['max_amount'];
$max_amount < $min_amount and $max_amount = $min_amount;
//make sure this level has donation
if (empty($donation)) {
$pmpro_msg = "Error: You tried to set the amount on a level that doesn't have donation. Please try again.";
$pmpro_msgt = "pmmpro_error";
}
//get amount
$amount = preg_replace("[^0-9\.]", "", $donation_amount);
//check that the price falls between the min and max
if ((double)$amount < (double)$min_amount) {
$pmpro_msg = "The lowest accepted donation amount is " . $pmpro_currency_symbol . $min_amount . ". Please enter a new amount.";
$pmpro_msgt = "pmmpro_error";
$continue = false;
} elseif ((double)$amount > (double)$max_amount) {
$pmpro_msg = "The highest accepted donation amount is " . $pmpro_currency_symbol . $max_amount . ". Please enter a new amount.";
$pmpro_msgt = "pmmpro_error";
$continue = false;
}
//all good!
}
}
return $continue;
}
// process additional products from shopping cart to get the total initial payment
function pmpro_checkout_order($order)
{
//check if the cart has items, otherwise do nothing
if (empty($order->membership_level->cart))
return $order;
//add membership amount to cart and recalculate initial payment, subtotal and tax
$order->membership_level->cart['membership'] = $order->InitialPayment;
$order->InitialPayment = 0;
foreach ($order->membership_level->cart as $item => $amount) {
$order->InitialPayment += $amount;
}
//tax
$order->subtotal = $order->InitialPayment;
$order->getTax(true);
$order->notes .= "[ITEMS]" . json_encode($order->membership_level->cart) . "[/ITEMS]";
return $order;
}
// save itemized order from shopping cart
function pmpro_added_order($order)
{
//check if the cart has items, otherwise do nothing
if (empty($order->membership_level->cart))
return;
// use $order->id for reference
foreach ($order->membership_level->cart as $item => $amount) {
$this->add_order_item($order->id, array('name' => $item, 'amount' => $amount));
}
}
function pmpro_invoice_bullets_bottom($invoice)
{
//get saved itemized order from notes
preg_match('/\[ITEMS]([^\[]*)\[\/ITEMS]/i', $invoice->notes, $items);
is_array($items) and $items = (array) json_decode($items[1]);
//$items should contain the array of items now
echo '<ul>';
foreach ($items as $item => $amount) {
echo "<li>$item = $amount</li>";
}
echo '</ul>';
}
//TODO: table to store product addons and order items
function create_tables()
{
/*
*
CREATE TABLE wp_pmpro_membership_orders_meta (
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
order_id bigint(20) NOT NULL,
meta_key varchar(255) DEFAULT NULL,
meta_value longtext,
PRIMARY KEY (meta_id),
KEY order_id (order_id),
KEY meta_key (meta_key)
)
*/
}
// check if user has no prior membership
function is_new_membership($user_id)
{
global $wpdb;
$table = $wpdb->pmpro_memberships_users;
$query = "SELECT user_id FROM ${table} WHERE user_id = ${user_id}";
return $wpdb->get_var($query) ? false : true;
}
private function add_order_item($order_id, array $item)
{
global $wpdb;
$sql = "INSERT INTO $wpdb->pmpro_membership_orders_meta (order_id, meta_key, meta_value) VALUES(" . $order_id . ", '" . $item['name'] . "', " . $item['amount'] . ")";
return $wpdb->query($sql);
}
}
//load pmpro products plugin
add_action('init', array('PMPro_Products', 'get_instance'));