-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwcs-restrict-product.php
executable file
·589 lines (506 loc) · 21.2 KB
/
wcs-restrict-product.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
/*
* Plugin Name: WooCommerce Subscriptions Restrict Product
* Plugin URI: https://github.com/Prospress/woocommerce-subscriptions-restrict-product/
* Description: Restricts subscription products to a certain number of total active (unended) subscriptions on a site.
* Author: Prospress Inc.
* Author URI: https://prospress.com/
* License: GPLv3
* Version: 1.0.2
* Requires at least: 4.0
* Tested up to: 4.9.2
*
* GitHub Plugin URI: Prospress/woocommerce-subscriptions-one-to-one
* GitHub Branch: master
*
* Copyright 2018 Prospress, Inc. (email : freedoms@prospress.com)
*
* 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 3 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package WooCommerce Subscriptions
* @author Prospress Inc.
* @since 1.0
*/
require_once( 'includes/class-pp-dependencies.php' );
if ( false === PP_Dependencies::is_woocommerce_active( '3.0' ) ) {
PP_Dependencies::enqueue_admin_notice( 'WooCommerce Subscriptions Restrict Product', 'WooCommerce', '3.0' );
return;
}
if ( false === PP_Dependencies::is_subscriptions_active( '2.1' ) ) {
PP_Dependencies::enqueue_admin_notice( 'WooCommerce Subscriptions Restrict Product', 'WooCommerce Subscriptions', '2.1' );
return;
}
// page to view the current cache
require_once( 'view-cache.php' );
function plugin_add_view_cache_link( $links ) {
$page_link = '<a href=' . admin_url( '?page=cache-display') . '>' . __( 'View cache' ) . '</a>';
array_push( $links, $page_link );
return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin", 'plugin_add_view_cache_link' );
// creates array of product IDs in the options table when plugin is activated
register_activation_hook( __FILE__,'create_wcs_restriction_cache' );
// deletes array when plugin is deactivated
register_deactivation_hook( __FILE__, 'cleanup_wcs_restriction_cache' );
// Append "Product Restriction" section in the Subscriptions settings tab.
add_filter( 'woocommerce_subscription_settings', 'restrict_product_admin_settings' );
// Save admin settings.
add_action( 'woocommerce_update_options_subscriptions', 'save_restrict_product_admin_settings' );
//Add product restriction option on edit product page
add_action( 'woocommerce_product_options_advanced', 'wcs_restriction_admin_edit_product_fields' );
// Save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'wcs_restriction_save_product_fields' );
// updates the array whenever subscription status is updated
add_filter( 'woocommerce_subscription_status_updated', 'update_wcs_restriction_cache', 10, 3 );
// updates the array whenever subscription is created through checkout
add_filter( 'woocommerce_checkout_subscription_created', 'add_to_wcs_restriction_cache' );
// refreshes cache whenever a subscription is edited or created in admin
add_action( 'woocommerce_process_shop_order_meta', 'refresh_wcs_restriction_cache', 60, 2 );
// Prevents purchase of restricted products, but still allows manual renewals and payment of failed renewal orders
add_filter( 'woocommerce_subscription_is_purchasable', 'wcs_restriction_is_purchasable_renewal', 11, 2 );
add_filter( 'woocommerce_subscription_variation_is_purchasable', 'wcs_restriction_is_purchasable_renewal', 11, 2 );
// bypasses restriction for switches only if switching to same variable product
add_filter( 'woocommerce_subscription_variation_is_purchasable', 'wcs_restriction_is_purchasable_switch', 12, 2 );
// when displaying product on front end, hides product if restricted, and if the 'Out of stock visibility' option is checked in WooCommerce settings -> products -> inventory tab
add_filter( 'woocommerce_product_is_visible', 'wcs_restricted_is_visible', 10, 2 );
// add max to quantity selector on product page
add_filter( 'woocommerce_quantity_input_max', 'wcs_restriction_quantity_input_max', 1, 2 );
// Validating the quantity on add to cart action with the quantity of the same product available in the cart.
add_filter( 'woocommerce_add_to_cart_validation', 'wcs_restriction_qty_add_to_cart_validation', 1, 3 );
// Validate product quantity on cart update.
add_filter( 'woocommerce_update_cart_validation', 'wcs_restriction_update_validate_quantity', 10, 4 );
/**
* creates array of product IDs in the options table when plugin is activated
*/
function create_wcs_restriction_cache() {
$products_with_subscription = [];
$unended_subscriptions = wcs_get_subscriptions( array(
'subscription_status' => array( 'active', 'pending', 'on-hold', 'pending-cancel' ),
'subscriptions_per_page' => -1,
) );
foreach ( $unended_subscriptions as $subscription ) {
foreach ( $subscription->get_items() as $item_id => $line_item ) {
$product_id = $line_item->get_product_id();
$quantity = $line_item->get_quantity();
$product = wc_get_product( $product_id );
if( $product->is_type( 'subscription' ) || $product->is_type( 'variable-subscription' ) ) {
if ( !array_key_exists( $product_id, $products_with_subscription ) ) {
$products_with_subscription[$product_id] = 0;
}
$products_with_subscription[$product_id] += $quantity;
}
}
}
update_option( 'wcs_restriction_cache', $products_with_subscription );
}
/**
* deletes array when plugin is deactivated
*/
function cleanup_wcs_restriction_cache() {
delete_option( 'wcs_restriction_cache' );
}
/**
* updates the array whenever subscription is created
*
* @param instance of a WC_Subscription object
*/
function add_to_wcs_restriction_cache( $subscription, $order = null, $recurring_cart = null ) {
$cache = get_option( 'wcs_restriction_cache' );
foreach ( $subscription->get_items() as $item_id => $line_item ) {
$product_id = $line_item->get_product_id();
$quantity = $line_item->get_quantity();
$product = wc_get_product( $product_id );
if( $product->is_type( 'subscription' ) || $product->is_type( 'variable-subscription' ) ) {
if ( !array_key_exists( $product_id, $cache ) ) {
$cache[$product_id] = 0;
}
$cache[$product_id] += $quantity;
}
}
update_option( 'wcs_restriction_cache', $cache );
}
/**
* updates the array whenever subscription status is updated
*
* @param instance of a WC_Subscription object
* @param string
* @param string
*/
function update_wcs_restriction_cache( $subscription, $new_status, $old_status ) {
$cache = get_option( 'wcs_restriction_cache' );
$unended_statuses = array( 'active', 'pending', 'on-hold', 'pending-cancel' );
if ( in_array( $new_status, $unended_statuses ) && !in_array( $old_status, $unended_statuses ) ) {
foreach ( $subscription->get_items() as $item_id => $line_item ) {
$product_id = $line_item->get_product_id();
$quantity = $line_item->get_quantity();
$product = wc_get_product( $product_id );
if( $product->is_type( 'subscription' ) || $product->is_type( 'variable-subscription' ) ) {
if ( ! array_key_exists( $product_id, $cache ) ) {
$cache[ $product_id ] = 0;
}
$cache[ $product_id ] += $quantity;
}
}
update_option( 'wcs_restriction_cache', $cache );
} elseif ( in_array( $old_status, $unended_statuses ) && !in_array( $new_status, $unended_statuses ) ) {
foreach ( $subscription->get_items() as $item_id => $line_item ) {
$product_id = $line_item->get_product_id();
$quantity = $line_item->get_quantity();
$product = wc_get_product( $product_id );
if( $product->is_type( 'subscription' ) || $product->is_type( 'variable-subscription' ) ) {
if ( $cache[$product_id] <= 1 ) {
unset( $cache[$product_id] ); // remove product from array completely
} elseif ( $cache[$product_id] > 1 ) {
$cache[$product_id] -= $quantity; // or just reduce the number
}
}
}
update_option( 'wcs_restriction_cache', $cache );
}
}
/**
* refreshes the cache whenever subscription is saved in admin
*
* @param int $post_id
* @param int $post
*/
function refresh_wcs_restriction_cache( $post_id, $post ) {
if ( 'shop_subscription' == $post->post_type ) {
cleanup_wcs_restriction_cache();
create_wcs_restriction_cache();
}
}
/**
* Append "Product Restriction" section in the Subscriptions settings tab.
*
* @param array $settings
* @return array
*/
function restrict_product_admin_settings( $settings ) {
// Insert before miscellaneous settings.
$misc_section_start = wp_list_filter( $settings, array( 'id' => 'woocommerce_subscriptions_miscellaneous', 'type' => 'title' ) );
$spliced_array = array_splice( $settings, key( $misc_section_start ), 0, array(
array(
'name' => __( 'Product Restriction', 'woocommerce-subscriptions' ),
'type' => 'title',
'desc' => 'Restrict all subscription products to a total number of subscriptions on your site. This is a default setting that can be overwritten by the product-level settings.',
'id' => 'wcs_restrict_product_options',
),
array(
'name' => __( 'Sitewide Product Restriction', 'woocommerce-subscriptions' ),
'id' => '_default_product_restriction',
'label' => __( 'Restrict all products', 'woocommerce-subscriptions' ),
'desc_tip' => 'Leave blank or set to "0" to deactivate.',
'placeholder' => '0',
'type' => 'number',
'custom_attributes' => array(
'step' => '1',
'min' => '0', )
),
array(
'type' => 'sectionend',
'id' => 'wcs_restrict_product_options',
),
) );
return $settings;
}
/**
* Save product restriction settings from the WooCommerce > Settings > Subscriptions administration screen.
*
* @return void
*/
function save_restrict_product_admin_settings() {
$default_product_restriction = $_POST['_default_product_restriction'];
update_option( '_default_product_restriction', esc_attr( $default_product_restriction ) );
}
/**
* Adds restrict product option to 'Edit Product' screen.
*/
function wcs_restriction_admin_edit_product_fields() {
global $post;
echo '<div class="options_group restrict_product show_if_subscription show_if_variable-subscription"><p><strong>Restrict Product</strong></p>';
woocommerce_wp_checkbox(
array(
'id' => '_product_restrict_YN',
'label' => __('Activate product-level restriction?', 'woocommerce-subscriptions' ),
'desc_tip' => 'true',
'description' => __( 'This will override any sitewide product restriction.', 'woocommerce-subscriptions' ),
'value' => get_post_meta( $post->ID, '_product_restrict_YN', true ),
)
);
woocommerce_wp_text_input( array(
'id' => '_product_restriction',
'label' => __( 'Product-level restriction', 'woocommerce-subscriptions' ),
'desc_tip' => 'true',
'description' => __( 'Restrict product to a total number of unended subscriptions on your site. Activate on product level and leave blank to have product ignore sitewide restriction.' ),
'placeholder' => '',
'type' => 'number',
'custom_attributes' => array(
'step' => '1',
'min' => '0', )
) );
echo '</div>';
do_action( 'woocommerce_subscriptions_product_options_advanced' );
}
/**
* Save the data value from the custom field
*
* @param int
*/
function wcs_restriction_save_product_fields( $post_id ) {
$product_restrict_YN = $_POST['_product_restrict_YN'];
update_post_meta( $post_id, '_product_restrict_YN', esc_attr( $product_restrict_YN ) );
$product_restriction = $_POST['_product_restriction'];
update_post_meta( $post_id, '_product_restriction', esc_attr( $product_restriction ) );
}
/**
* helper function to check product-level restriction, then fall back to default
*
* @param integer
* @return integer
*/
function wcs_restrict_product_get_restriction( $product_id ) {
$product_restrict_YN = get_post_meta( $product_id, '_product_restrict_YN', true );
$product_restriction = get_post_meta( $product_id, '_product_restriction', true );
if ( 'yes' != $product_restrict_YN ) {
$product_restriction = get_option( '_default_product_restriction', 0 );
} elseif ( !isset( $product_restriction ) || empty( $product_restriction ) || 0 == $product_restriction ) {
$product_restriction = 0;
}
return $product_restriction;
}
/**
* main helper function that makes it not purchasable by checking against cache to see if subscription is currently restricted
*
* @param boolean
* @param integer
* @return boolean
*/
function wcs_restricted_is_purchasable( $is_purchasable, $id ) {
if ( $is_purchasable ) {
$cache = get_option( 'wcs_restriction_cache' );
$product_restriction_option = wcs_restrict_product_get_restriction( $id );
// uncomment the following lines to print the current cache
// $log = new WC_Logger();
// $log_entry = 'Cache: ' . print_r( $cache, TRUE);
// $log->add( 'wcs-restrict-product-debug', $log_entry );
if ( $cache != false ) {
if ( isset($cache[$id] ) && ( $product_restriction_option > 0 ) && ( $cache[$id] >= $product_restriction_option ) ) {
$is_purchasable = false;
}
}
}
return $is_purchasable;
}
/**
* Determines whether a product is purchasable based on whether the cart is set to resubscribe or renew.
*
* @param boolean
* @param instance of WC_Product object
* @return boolean
*/
function wcs_restriction_is_purchasable_renewal( $is_purchasable, $product ) {
// check if restricted first
$is_purchasable = wcs_restricted_is_purchasable( $is_purchasable, $product->get_id() );
// then, allow to be purchased if renewal or resubscribe
if ( false === $is_purchasable ) {
// Resubscribe logic
if ( isset( $_GET['resubscribe'] ) || false !== ( $resubscribe_cart_item = wcs_cart_contains_resubscribe() ) ) {
$subscription_id = ( isset( $_GET['resubscribe'] ) ) ? absint( $_GET['resubscribe'] ) : $resubscribe_cart_item['subscription_resubscribe']['subscription_id'];
$subscription = wcs_get_subscription( $subscription_id );
if ( false != $subscription && $subscription->has_product( $product->get_id() ) && wcs_can_user_resubscribe_to( $subscription ) && 'pending-cancel' != $subscription->get_status() ) {
$is_purchasable = true;
}
// Renewal logic
} elseif ( isset( $_GET['subscription_renewal'] ) || wcs_cart_contains_renewal() ) {
$is_purchasable = true;
// Restoring cart from session, so need to check the cart in the session (wcs_cart_contains_renewal() only checks the cart)
} elseif ( WC()->session->cart ) {
foreach ( WC()->session->cart as $cart_item_key => $cart_item ) {
if ( $product->get_id() == $cart_item['product_id'] && ( isset( $cart_item['subscription_renewal'] ) || isset( $cart_item['subscription_resubscribe'] ) ) ) {
$is_purchasable = true;
break;
}
}
}
}
return $is_purchasable;
}
/**
* when displaying product on front end, hides product if restricted, and if the 'Out of stock visibility' option is checked in WooCommerce settings -> products -> inventory tab
*
* @param boolean
* @param integer
* @return boolean
*/
function wcs_restricted_is_visible( $is_visible, $id ) {
if ( $is_visible ) {
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
$is_visible = wcs_restricted_is_purchasable( $is_visible, $id );
}
}
return $is_visible;
}
/**
* bypasses restriction for switches only if switching to same variable product
*
* @param boolean
* @param instance of WC_Product object
* @return boolean
*/
function wcs_restriction_is_purchasable_switch( $is_purchasable, $product ) {
// Check if the product is restricted first
$is_purchasable_check = wcs_restricted_is_purchasable( $is_purchasable, $product->get_parent_id() );
// We're only concerned with variable products during switch because individual grouped products should be handled on their own.
if ( false === $is_purchasable_check && 'subscription_variation' === $product->get_type() ) {
// If the customer has requested to switch
if ( isset( $_GET['switch-subscription'] ) ) {
// check if the product is a variation of the same parent product
$subscription = wcs_get_subscription( $_GET['switch-subscription'] );
$line_item_id = $_GET['item'];
foreach ( $subscription->get_items() as $item_id => $item_values ) {
$product_id = $item_values->get_product_id();
if ( ( $line_item_id == $item_id ) && ( $product_id == $product->get_parent_id() ) ) {
$is_purchasable = true;
}
}
// If the cart contains a switch to this product
} elseif ( WC_Subscriptions_Switcher::cart_contains_switch_for_product( $product ) ) {
$is_purchasable = true;
// If restoring cart from session, the cart doesn't exist so cart_contains_switch_for_product will fail.
} elseif ( isset( WC()->session->cart ) ) {
foreach ( WC()->session->cart as $cart_item_key => $cart_item ) {
if ( $product->get_parent_id() == $cart_item['product_id'] && isset( $cart_item['subscription_switch'] ) ) {
$is_purchasable = true;
break;
}
}
}
}
return $is_purchasable;
}
/**
* helper function that gets quantity of given product in the cart
*
* @param int
* @return int
*/
function get_product_quantity_in_cart( $product_id ) {
$quantity = 0;
if ( ! empty( WC()->cart->cart_contents ) ) {
foreach ( WC()->cart->cart_contents as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
$quantity += $cart_item['quantity'];
}
}
}
return $quantity;
}
/**
* add max to quantity selector on product page
*
* @param int
* @param instance of WC_Product object
* @return int
*/
function wcs_restriction_quantity_input_max( $max, $product ) {
$product_restriction_option = $num_active_subs_for_product = $quantity_in_cart = 0;
// (the total number allowed - the total quantity of active subscriptions to this product - number of products in the cart)
$product_restriction_option = wcs_restrict_product_get_restriction( $product->get_id() );
if ( 0 == $product_restriction_option ) {
$max = 9999;
} else {
$cache = ( null !== get_option( 'wcs_restriction_cache' ) ? get_option( 'wcs_restriction_cache' ) : 0);
$num_active_subs_for_product = ( isset( $cache[$product->get_id()] ) ? $cache[$product->get_id()] : 0);
$quantity_in_cart = get_product_quantity_in_cart( $product->get_id() );
$max = $product_restriction_option - $num_active_subs_for_product - $quantity_in_cart;
}
return $max;
};
/**
* Validating the quantity on add to cart action with the quantity of the same product available in the cart.
*
* @param boolean
* @param int
* @param int
* @param int
* @param array
* @return boolean
*/
function wcs_restriction_qty_add_to_cart_validation( $passed, $product_id, $quantity) {
// this applies to checkout when renewing/resubscribing too, so we need to bypass it in those cases
if ( ( isset( $_GET['resubscribe'] ) || false !== ( $resubscribe_cart_item = wcs_cart_contains_resubscribe() ) || isset( $_GET['subscription_renewal'] ) || wcs_cart_contains_renewal()) ) {
return $passed;
} elseif ( WC()->session->cart ) {
foreach ( WC()->session->cart as $cart_item_key => $cart_item ) {
if ( $product_id == $cart_item['product_id'] && ( isset( $cart_item['subscription_renewal'] ) || isset( $cart_item['subscription_resubscribe'] ) ) ) {
return $passed;
}
}
}
$product = wc_get_product( $product_id );
if ( !$product->is_type( 'subscription' ) && !$product->is_type( 'variable-subscription' ) ) {
return $passed;
}
$product_max = wcs_restriction_quantity_input_max( 0, $product );
if ( ! empty( $product_max ) ) {
if ( false == $product_max ) {
return $passed;
}
}
if ( $quantity > $product_max ) {
$passed = false;
wc_add_notice( apply_filters( 'isa_wc_max_qty_error_message_already_had', sprintf( __( 'This product is restricted. You can add a maximum of %1$s more to %2$s.', 'woocommerce-max-quantity' ),
$product_max,
'<a href="' . esc_url( wc_get_cart_url() ) . '">' . __( 'your cart', 'woocommerce-max-quantity' ) . '</a>'),
$product_max),
'error' );
}
return $passed;
}
/**
* Validate product quantity on cart update.
*
* @param boolean
* @param string
* @param array
* @param int
* @return boolean
*/
function wcs_restriction_update_validate_quantity( $passed, $cart_item_key, $values, $quantity ) {
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
$product_id = $cart_item['product_id'];
$product = wc_get_product( $product_id );
$product_restriction_option = $num_active_subs_for_product = $quantity_in_cart = 0;
$product_restriction_option = wcs_restrict_product_get_restriction( $product_id );
$cache = ( null !== get_option( 'wcs_restriction_cache' ) ? get_option( 'wcs_restriction_cache' ) : 0 );
$num_active_subs_for_product = ( isset( $cache[$product_id] ) ? $cache[$product_id] : 0 );
$quantity_in_cart = get_product_quantity_in_cart( $product->get_parent_id() );
$product_max = $product_restriction_option - $num_active_subs_for_product - $quantity_in_cart;
if ( ! empty( $product_max ) ) {
if ( false == $product_max ) {
return $passed;
}
}
if ( $quantity > $product_max ) {
$passed = false;
wc_add_notice( apply_filters( 'isa_wc_max_qty_error_message_already_had', sprintf( __( 'This product is restricted. You can have a maximum of %1$s in %2$s.', 'woocommerce-max-quantity' ),
$product_max,
'<a href="' . esc_url( wc_get_cart_url() ) . '">' . __( 'your cart', 'woocommerce-max-quantity' ) . '</a>'),
$product_max),
'error' );
}
return $passed;
}