-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-min-max-quantities.php
1776 lines (1441 loc) · 62 KB
/
woocommerce-min-max-quantities.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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Woo Min/Max Quantities
* Plugin URI: https://woocommerce.com/products/minmax-quantities/
* Description: Define minimum/maximum allowed quantities for products, variations and orders.
* Version: 5.0.1
* Author: Woo
* Author URI: https://woocommerce.com
*
* Requires PHP: 7.4
*
* Requires at least: 6.2
* Tested up to: 6.6
*
* WC tested up to: 9.1
* WC requires at least: 8.2
*
* Requires Plugins: woocommerce
*
* Text Domain: woocommerce-min-max-quantities
* Domain Path: /languages
*
* Copyright: © 2022 WooCommerce
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Woo: 18616:2b5188d90baecfb781a5aa2d6abb900a
*
* @package woocommerce-min-max-quantities
*/
if ( ! class_exists( 'WC_Min_Max_Quantities' ) ) :
define( 'WC_MIN_MAX_QUANTITIES', '5.0.1' ); // WRCS: DEFINED_VERSION.
/**
* Min Max Quantities class.
*/
class WC_Min_Max_Quantities {
/**
* Minimum WooCommerce version.
*
* @var string
*/
public $min_wc_version = '8.2.0';
/**
* Minimum order quantity.
*
* @var int
*/
public $minimum_order_quantity;
/**
* Maximum order quantity.
*
* @var int
*/
public $maximum_order_quantity;
/**
* Minimum order value.
*
* @var int
*/
public $minimum_order_value;
/**
* Maximum order value.
*
* @var int
*/
public $maximum_order_value;
/**
* List of excluded product titles.
*
* @var array
*/
public $excludes = array();
/**
* Instance of compatibility class.
*
* @var WC_MMQ_Compatibility
*/
public $compatibility;
/**
* Instance of addons class.
*
* @var WC_Min_Max_Quantities_Addons
*/
public $addons;
/**
* Class instance.
*
* @var object
*/
private static $instance;
/**
* Get the class instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
public function __construct() {
if ( ! function_exists( 'WC' ) || version_compare( WC()->version, $this->min_wc_version ) < 0 ) {
add_action( 'admin_notices', array( $this, 'woocommerce_required_notice' ) );
return;
}
if ( ! function_exists( 'phpversion' ) || version_compare( phpversion(), '7.4.0', '<' ) ) {
add_action( 'admin_notices', array( $this, 'php_version_notice' ) );
}
$this->maybe_define_constant( 'WC_MMQ_ABSPATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
$this->maybe_define_constant( 'WC_MMQ_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/**
* Localisation.
*/
$this->load_plugin_textdomain();
if ( is_admin() ) {
include_once WC_MMQ_ABSPATH . 'includes/admin/class-wc-min-max-quantities-admin.php';
include_once WC_MMQ_ABSPATH . 'includes/admin/class-wc-min-max-quantities-admin-notices.php';
}
add_action( 'init', array( $this, 'maybe_install' ) );
// Blocks.
include_once WC_MMQ_ABSPATH . 'includes/class-wc-min-max-quantities-blocks.php';
// REST API hooks.
include_once WC_MMQ_ABSPATH . 'includes/api/class-wc-mmq-rest-api.php';
// Extensions compatibility functions and hooks.
include_once WC_MMQ_ABSPATH . 'includes/compatibility/class-wc-min-max-quantities-compatibility.php';
$this->compatibility = WC_MMQ_Compatibility::instance();
if ( $this->compatibility->is_module_loaded( 'product_addons' ) ) {
$this->addons = new WC_Min_Max_Quantities_Addons();
}
$this->minimum_order_quantity = absint( get_option( 'woocommerce_minimum_order_quantity' ) );
$this->maximum_order_quantity = absint( get_option( 'woocommerce_maximum_order_quantity' ) );
$this->minimum_order_value = absint( get_option( 'woocommerce_minimum_order_value' ) );
$this->maximum_order_value = absint( get_option( 'woocommerce_maximum_order_value' ) );
// Check items.
add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ) );
// If we have errors, make sure those are shown on the checkout page
add_action( 'woocommerce_cart_has_errors', array( $this, 'output_errors' ) );
// Quantity selelectors (2.0+).
add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_quantity_args' ), 10, 2 );
add_filter( 'woocommerce_available_variation', array( $this, 'available_variation' ), 10, 3 );
add_filter( 'wc_min_max_use_group_as_min_quantity', array( $this, 'use_group_as_min_quantity' ), 10, 3 );
// Prevent add to cart.
add_filter( 'woocommerce_add_to_cart_validation', array( $this, 'add_to_cart' ), 10, 4 );
// Min add to cart ajax.
add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'add_to_cart_link' ), 10, 2 );
// Show a notice when items would have to be on back order because of min/max.
add_filter( 'woocommerce_get_availability', array( $this, 'maybe_show_backorder_message' ), 10, 2 );
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
add_filter( 'woocommerce_add_to_cart_product_id', array( $this, 'modify_add_to_cart_quantity' ) );
add_action( 'before_woocommerce_init', array( $this, 'declare_feature_compatibilities' ) );
add_action( 'init', array( $this, 'register_custom_taxonomies' ) );
}
/**
* Sets DB version and conditionally displays welcome notice.
*
* @since 4.2.3
*/
public function maybe_install() {
if ( is_null( get_option( 'woocommerce_mmq_version', null ) ) ) {
include_once WC_MMQ_ABSPATH . 'includes/admin/class-wc-min-max-quantities-admin-notices.php';
WC_Min_Max_Quantities_Admin_Notices::add_maintenance_notice( 'welcome' );
add_option( 'woocommerce_mmq_version', WC_MIN_MAX_QUANTITIES );
}
}
/**
* Registers custom taxonomies.
*
* @since 4.1.4
*/
public function register_custom_taxonomies() {
register_meta(
'term',
'group_of_quantity',
array(
'show_in_rest' => true,
'type' => 'string',
'single' => true,
)
);
}
/**
* Plugin version getter.
*
* @since 4.3.0
*
* @param boolean $base
* @param string $version
* @return string
*/
public function plugin_version( $base = false, $version = '' ) {
$version = $version ? $version : WC_MIN_MAX_QUANTITIES;
if ( $base ) {
$version_parts = explode( '-', $version );
$version = count( $version_parts ) > 1 ? $version_parts[ 0 ] : $version;
}
return $version;
}
/**
* Define constants if not present.
*
* @since 4.0.4
*
* @return boolean
*/
protected function maybe_define_constant( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Plugin URL getter.
*
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Output a notice if Woocommerce isn't active.
*/
public function woocommerce_required_notice() {
?><div class="notice notice-error is-dismissible">
<p>
<?php
/* translators: Minimum required WooCommerce version */
echo wp_kses_post(
sprintf(
__(
'<strong>Min/Max Quantities</strong> requires at least WooCommerce <strong>%s</strong>.',
'woocommerce-min-max-quantities' ),
esc_html( $this->min_wc_version )
)
);
?>
</p>
</div><?php
}
/**
* Output a notice if PHP is older than 7.0
*/
public function php_version_notice() {
?><div class="notice notice-error is-dismissible">
<p>
<?php
/* translators: Minimum required PHP version */
echo wp_kses_post( sprintf( __( 'Woo Min/Max Quantities requires at least PHP <strong>%1$s</strong>. Learn <a href="%2$s">how to update PHP</a>.', 'woocommerce-min-max-quantities' ), '7.4.0', 'https://woocommerce.com/document/how-to-update-your-php-version/' ) );
?>
</p>
</div><?php
}
/**
* Load scripts.
*/
public function load_scripts() {
// Only load on single product page and cart page.
if ( is_product() || is_cart() ) {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_register_script( 'wc-mmq-frontend', $this->plugin_url() . '/assets/js/frontend/validate' . $suffix . '.js', array( 'jquery' ), WC_MIN_MAX_QUANTITIES );
wp_script_add_data( 'wc-mmq-frontend', 'strategy', 'defer' );
wp_enqueue_script( 'wc-mmq-frontend' );
}
}
/**
* Declare HPOS (Custom Order tables), Blocks and new Product Editor compatibility.
*
* @since 4.0.2
*/
public function declare_feature_compatibilities () {
if ( ! class_exists( 'Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
return;
}
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__ );
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__ );
if (apply_filters( 'min_max_new_product_editor_enabled', '__return_false' )) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'product_block_editor', __FILE__ );
}
}
/**
* Load Localisation files.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Frontend/global Locales found in:
* - WP_LANG_DIR/woocommerce-min-max-quantities/woocommerce-min-max-quantities-LOCALE.mo
* - woocommerce-min-max-quantities/woocommerce-min-max-quantities-LOCALE.mo (which if not found falls back to:)
* - WP_LANG_DIR/plugins/woocommerce-min-max-quantities-LOCALE.mo
*/
public function load_plugin_textdomain() {
// phpcs:ignore
$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-min-max-quantities' );
load_textdomain( 'woocommerce-min-max-quantities', WP_LANG_DIR . '/woocommerce-min-max-quantities/woocommerce-min-max-quantities-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce-min-max-quantities', false, plugin_basename( dirname( __FILE__ ) ) . '/' );
}
/**
* Add an error.
*
* @since 1.0.0
* @version 2.3.18
* @param string $error Error text.
*/
public function add_error( $error = '' ) {
if ( $error && ! wc_has_notice( $error, 'error' ) ) {
wc_add_notice( $error, 'error', array( 'source' => 'woocommerce-min-max-quantities' ) );
}
}
/**
* Output any plugin specific error messages
*
* We use this instead of wc_print_notices so we
* can remove any error notices that aren't from us.
*/
public function output_errors() {
$notices = wc_get_notices( 'error' );
$messages = array();
foreach ( $notices as $i => $notice ) {
if ( isset( $notice['notice'] ) && isset( $notice['data']['source'] ) && 'woocommerce-min-max-quantities' === $notice['data']['source'] ) {
$messages[] = $notice['notice'];
} else {
unset( $notice[ $i ] );
}
}
if ( ! empty( $messages ) ) {
ob_start();
wc_get_template(
'notices/error.php',
array(
'messages' => array_filter( $messages ), // @deprecated 3.9.0
'notices' => array_filter( $notices ),
)
);
echo wc_kses_notice( ob_get_clean() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Add quantity property to add to cart button on shop loop for simple products.
*
* @param string $html Add to cart link.
* @param WC_Product $product Product object.
* @return string
*/
public function add_to_cart_link( $html, $product ) {
if ( 'variable' !== $product->get_type() ) {
$quantity_attribute = 1;
$minimum_quantity = absint( get_post_meta( $product->get_id(), 'minimum_allowed_quantity', true ) );
$group_of_quantity = $this->get_group_of_quantity_for_product( $product );
if ( $minimum_quantity || $group_of_quantity ) {
$quantity_attribute = $minimum_quantity;
if ( $group_of_quantity > 0 && $minimum_quantity < $group_of_quantity ) {
$quantity_attribute = $group_of_quantity;
}
$html = str_replace( '<a ', '<a data-quantity="' . $quantity_attribute . '" ', $html );
}
}
return $html;
}
/**
* Get product or variation ID to check
*
* @param array $values List of values.
* @return int
*/
public function get_id_to_check( $values ) {
if ( $values['variation_id'] ) {
$min_max_rules = get_post_meta( $values['variation_id'], 'min_max_rules', true );
$allow_combination = ( 'yes' === get_post_meta( $values['product_id'], 'allow_combination', true ) );
if ( 'yes' === $min_max_rules && ! $allow_combination ) {
$checking_id = $values['variation_id'];
} else {
$checking_id = $values['product_id'];
}
} else {
$checking_id = $values['product_id'];
}
return $checking_id;
}
/**
* Validate cart items against set rules
*
* @throws Exception
*/
public function check_cart_items() {
try {
$checked_ids = array();
$product_quantities = array();
$category_quantities = array();
$total_quantity = 0;
$total_cost = 0;
$apply_cart_rules = false;
// Count items + variations first.
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$product = $values[ 'data' ];
$checking_id = $this->get_id_to_check( $values );
/**
* Use this filter to prevent the quantity of a cart item from being counted in cart-level rules.
*
* @since 2.4.11
*
* @param boolean $do_not_count
* @param int $checking_id
* @param string $cart_item_key
* @param array $values
*/
if ( apply_filters( 'wc_min_max_cart_quantity_do_not_count', false, $checking_id, $cart_item_key, $values ) ) {
$values[ 'quantity' ] = 0;
}
if ( ! isset( $product_quantities[ $checking_id ] ) ) {
$product_quantities[ $checking_id ] = $values[ 'quantity' ];
} else {
$product_quantities[ $checking_id ] += $values[ 'quantity' ];
}
/**
* Use this filter to prevent the quantity or cost of a cart item from being counted in cart-level rules.
*
* @since 2.3.6
*
* @param boolean $do_not_count
* @param int $checking_id
* @param string $cart_item_key
* @param array $values
*/
$minmax_do_not_count = apply_filters( 'wc_min_max_quantity_minmax_do_not_count', ( 'yes' === get_post_meta( $checking_id, 'variation_minmax_do_not_count', true ) ? 'yes' : get_post_meta( $values[ 'product_id' ], 'minmax_do_not_count', true ) ), $checking_id, $cart_item_key, $values );
/**
* Use this filter to exclude a product from cart-level rules.
*
* @since 2.3.6
*
* @param boolean $exclude
* @param int $checking_id
* @param string $cart_item_key
* @param array $values
*/
$minmax_cart_exclude = apply_filters( 'wc_min_max_quantity_minmax_cart_exclude', ( 'yes' === get_post_meta( $checking_id, 'variation_minmax_cart_exclude', true ) ? 'yes' : get_post_meta( $values[ 'product_id' ], 'minmax_cart_exclude', true ) ), $checking_id, $cart_item_key, $values );
if ( 'yes' !== $minmax_do_not_count && 'yes' !== $minmax_cart_exclude ) {
$total_cost += (float) $product->get_price() * (float) $values['quantity'];
}
}
// Check cart items.
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$checking_id = $this->get_id_to_check( $values );
$terms = get_the_terms( $values[ 'product_id' ], 'product_cat' );
$found_term_ids = array();
// If a product belongs to multiple categories with different 'Group of' values, find the category with the smallest 'Group of' value.
$min_group_of_category_id = 0;
$min_group_of_category_value = 0;
if ( $terms ) {
foreach ( $terms as $term ) {
if ( 'yes' === get_post_meta( $checking_id, 'variation_minmax_category_group_of_exclude', true ) || 'yes' === get_post_meta( $values[ 'product_id' ], 'minmax_category_group_of_exclude', true ) ) {
continue;
}
if ( in_array( $term->term_id, $found_term_ids, true ) ) {
continue;
}
$found_term_ids[] = $term->term_id;
$category_group_of_value = absint( get_term_meta( $term->term_id, 'group_of_quantity', true ) );
if ( 0 !== $category_group_of_value && ( 0 === $min_group_of_category_value || $category_group_of_value < $min_group_of_category_value ) ) {
$min_group_of_category_value = $category_group_of_value;
$min_group_of_category_id = $term->term_id;
}
// Record count in parents of this category too.
$parents = get_ancestors( $term->term_id, 'product_cat' );
foreach ( $parents as $parent ) {
if ( in_array( $parent, $found_term_ids, true ) ) {
continue;
}
$found_term_ids[] = $parent;
$category_group_of_value = absint( get_term_meta( $parent, 'group_of_quantity', true ) );
if ( 0 !== $category_group_of_value && ( 0 === $min_group_of_category_value || $category_group_of_value < $min_group_of_category_value ) ) {
$min_group_of_category_value = $category_group_of_value;
$min_group_of_category_id = $parent;
}
}
}
}
if ( 0 !== $min_group_of_category_id && 0 !== $min_group_of_category_value ) {
$category_quantities[ $min_group_of_category_id ] = isset( $category_quantities[ $min_group_of_category_id ] )
? $category_quantities[ $min_group_of_category_id ] + $values[ 'quantity' ]
: $values[ 'quantity' ];
}
// Check item rules once per product ID.
if ( in_array( $checking_id, $checked_ids, true ) ) {
continue;
}
$product = $values[ 'data' ];
/**
* Use this filter to prevent the quantity or cost of a cart item from being counted in cart-level rules.
*
* @since 2.3.6
*
* @param boolean $do_not_count
* @param int $checking_id
* @param string $cart_item_key
* @param array $values
*/
$minmax_do_not_count = apply_filters( 'wc_min_max_quantity_minmax_do_not_count', ( 'yes' === get_post_meta( $checking_id, 'variation_minmax_do_not_count', true ) ? 'yes' : get_post_meta( $values[ 'product_id' ], 'minmax_do_not_count', true ) ), $checking_id, $cart_item_key, $values );
/**
* Use this filter to exclude a product from cart-level rules.
*
* @since 2.3.6
*
* @param boolean $exclude
* @param int $checking_id
* @param string $cart_item_key
* @param array $values
*/
$minmax_cart_exclude = apply_filters( 'wc_min_max_quantity_minmax_cart_exclude', ( 'yes' === get_post_meta( $checking_id, 'variation_minmax_cart_exclude', true ) ? 'yes' : get_post_meta( $values[ 'product_id' ], 'minmax_cart_exclude', true ) ), $checking_id, $cart_item_key, $values );
if ( 'yes' === $minmax_do_not_count || 'yes' === $minmax_cart_exclude ) {
// Do not count.
$this->excludes[] = $product->get_name();
} else {
$total_quantity += $product_quantities[ $checking_id ];
}
if ( 'yes' !== $minmax_cart_exclude ) {
$apply_cart_rules = true;
}
$checked_ids[] = $checking_id;
if ( $values[ 'variation_id' ] ) {
$min_max_rules = get_post_meta( $values[ 'variation_id' ], 'min_max_rules', true );
$allow_combination = 'yes' === get_post_meta( $values[ 'product_id' ], 'allow_combination', true );
// Variation level min max rules enabled.
if ( 'yes' === $min_max_rules && ! $allow_combination ) {
/**
* Use this filter to filter the Minimum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $variation_id
* @param string $cart_item_key
* @param array $cart_item
*/
$minimum_quantity = absint( apply_filters( 'wc_min_max_quantity_minimum_allowed_quantity', get_post_meta( $values[ 'variation_id' ], 'variation_minimum_allowed_quantity', true ), $values[ 'variation_id' ], $cart_item_key, $values ) );
/**
* Use this filter to filter the Maximum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $variation_id
* @param string $cart_item_key
* @param array $cart_item
*/
$maximum_quantity = absint( apply_filters( 'wc_min_max_quantity_maximum_allowed_quantity', get_post_meta( $values[ 'variation_id' ], 'variation_maximum_allowed_quantity', true ), $values[ 'variation_id' ], $cart_item_key, $values ) );
/**
* Use this filter to filter the Group of quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $variation_id
* @param string $cart_item_key
* @param array $cart_item
*/
$group_of_quantity = absint( apply_filters( 'wc_min_max_quantity_group_of_quantity', get_post_meta( $values[ 'variation_id' ], 'variation_group_of_quantity', true ), $values[ 'variation_id' ], $cart_item_key, $values ) );
} else {
/**
* Use this filter to filter the Minimum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$minimum_quantity = absint( apply_filters( 'wc_min_max_quantity_minimum_allowed_quantity', get_post_meta( $values[ 'product_id' ], 'minimum_allowed_quantity', true ), $values[ 'product_id' ], $cart_item_key, $values ) );
/**
* Use this filter to filter the Maximum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$maximum_quantity = absint( apply_filters( 'wc_min_max_quantity_maximum_allowed_quantity', get_post_meta( $values[ 'product_id' ], 'maximum_allowed_quantity', true ), $values[ 'product_id' ], $cart_item_key, $values ) );
/**
* Use this filter to filter the Group of quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$group_of_quantity = absint( apply_filters( 'wc_min_max_quantity_group_of_quantity', get_post_meta( $values[ 'product_id' ], 'group_of_quantity', true ), $values[ 'product_id' ], $cart_item_key, $values ) );
}
} else {
/**
* Use this filter to filter the Minimum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$minimum_quantity = absint( apply_filters( 'wc_min_max_quantity_minimum_allowed_quantity', get_post_meta( $checking_id, 'minimum_allowed_quantity', true ), $checking_id, $cart_item_key, $values ) );
/**
* Use this filter to filter the Maximum Quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$maximum_quantity = absint( apply_filters( 'wc_min_max_quantity_maximum_allowed_quantity', get_post_meta( $checking_id, 'maximum_allowed_quantity', true ), $checking_id, $cart_item_key, $values ) );
/**
* Use this filter to filter the Group of quantity of a product/variation.
*
* @since 2.2.7
*
* @param string $quantity
* @param int $product_id
* @param string $cart_item_key
* @param array $cart_item
*/
$group_of_quantity = absint( apply_filters( 'wc_min_max_quantity_group_of_quantity', get_post_meta( $checking_id, 'group_of_quantity', true ), $checking_id, $cart_item_key, $values ) );
}
$this->check_rules( $product, $product_quantities[ $checking_id ], $minimum_quantity, $maximum_quantity, $group_of_quantity, $checking_id );
}
// Cart rules.
if ( $apply_cart_rules ) {
$excludes = '';
if ( count( $this->excludes ) > 0 ) {
$excludes = ' (' . __( 'excludes ', 'woocommerce-min-max-quantities' ) . implode( ', ', $this->excludes ) . ')';
}
if ( $this->minimum_order_quantity > 0 && $total_quantity < $this->minimum_order_quantity ) {
/* translators: %d: Minimum amount of items in the cart */
$notice = sprintf( __( 'To place an order, your cart must contain at least %d items.', 'woocommerce-min-max-quantities' ), $this->minimum_order_quantity ) . $excludes;
throw new Exception( $notice );
}
if ( $this->maximum_order_quantity > 0 && $total_quantity > $this->maximum_order_quantity ) {
/* translators: %d: Maximum amount of items in the cart */
$notice = sprintf( __( 'Your cart must not contain more than %d items to place an order.', 'woocommerce-min-max-quantities' ), $this->maximum_order_quantity );
throw new Exception( $notice );
}
// Check cart value.
if ( $this->minimum_order_value && $total_cost < $this->minimum_order_value ) {
/* translators: %s: Minimum order value */
$notice = sprintf( __( 'To place an order, your cart total must be at least %s.', 'woocommerce-min-max-quantities' ), wc_price( $this->minimum_order_value ) ) . $excludes;
throw new Exception( $notice );
}
if ( $this->maximum_order_value && $total_cost > $this->maximum_order_value ) {
/* translators: %s: Maximum order value */
$notice = sprintf( __( 'Your cart total must not be higher than %s to place an order.', 'woocommerce-min-max-quantities' ), wc_price( $this->maximum_order_value ) );
throw new Exception( $notice );
}
}
// Check category rules.
foreach ( $category_quantities as $category => $quantity ) {
$group_of_quantity = get_term_meta( $category, 'group_of_quantity', true );
if ( $group_of_quantity > 0 && ( intval( $quantity ) % intval( $group_of_quantity ) > 0 ) ) {
$term = get_term_by( 'id', $category, 'product_cat' );
$product_names = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
// If exclude is enable, skip.
if ( 'yes' === get_post_meta( $values[ 'product_id' ], 'minmax_category_group_of_exclude', true ) || 'yes' === get_post_meta( $values[ 'variation_id' ], 'variation_minmax_category_group_of_exclude', true ) ) {
continue;
}
if ( has_term( $category, 'product_cat', $values[ 'product_id' ] ) ) {
$product_names[] = $values[ 'data' ]->get_title();
}
}
if ( $product_names ) {
/* translators: %1$s: Category name, %2$s: Comma separated list of product names, %3$d: Group amount */
$notice = sprintf( __( 'Products in the <strong>%1$s</strong> category (<em>%2$s</em>) must be bought in multiples of %3$d.', 'woocommerce-min-max-quantities' ), $term->name, implode( ', ', $product_names ), $group_of_quantity, $group_of_quantity - ( $quantity % $group_of_quantity ) );
throw new Exception( $notice );
}
}
}
} catch ( Exception $e ) {
if ( WC_MMQ_Core_Compatibility::is_store_api_request() ) {
throw $e;
} else {
$notice = $e->getMessage();
if ( $notice ) {
wc_add_notice( $notice, 'error' );
}
}
}
}
/**
* If the minimum allowed quantity for purchase is lower then the current stock, we need to
* let the user know that they are on backorder, or out of stock.
*
* @param array $args List of arguments.
* @param WC_Product $product Product object.
*/
public function maybe_show_backorder_message( $args, $product ) {
if ( ! $product->managing_stock() ) {
return $args;
}
// Figure out what our minimum_quantity is.
$product_id = $product->get_id();
if ( 'WC_Product_Variation' === get_class( $product ) ) {
$variation_id = $product->get_id();
$min_max_rules = get_post_meta( $variation_id, 'min_max_rules', true );
if ( 'yes' === $min_max_rules ) {
$minimum_quantity = absint( get_post_meta( $variation_id, 'variation_minimum_allowed_quantity', true ) );
} else {
$minimum_quantity = absint( get_post_meta( $product->get_parent_id(), 'minimum_allowed_quantity', true ) );
}
} else {
$minimum_quantity = absint( get_post_meta( $product_id, 'minimum_allowed_quantity', true ) );
}
// If the minimum quantity allowed for purchase is smaller then the amount in stock, we need
// clearer messaging.
if ( $minimum_quantity > 0 && $product->get_stock_quantity() < $minimum_quantity ) {
if ( $product->backorders_allowed() ) {
return array(
'availability' => __( 'Available on backorder', 'woocommerce-min-max-quantities' ),
'class' => 'available-on-backorder',
);
} else {
return array(
'availability' => __( 'Out of stock', 'woocommerce-min-max-quantities' ),
'class' => 'out-of-stock',
);
}
}
return $args;
}
/**
* Add respective error message depending on rules checked.
*
* @throws Exception
*
* @param WC_Product $product Product object.
* @param int $quantity Quantity to check.
* @param int $minimum_quantity Minimum quantity.
* @param int $maximum_quantity Maximum quanitty.
* @param int $group_of_quantity Group quantity.
* @param int|null $checking_id Variation ID
* @return void
*/
public function check_rules( $product, $quantity, $minimum_quantity, $maximum_quantity, $group_of_quantity, $checking_id = null ) {
if ( $product->is_sold_individually() ) {
return;
}
try {
$parent_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
$variation_title = $checking_id ? get_the_title( $checking_id ) : $product->get_title();
$allow_combination = 'yes' === get_post_meta( $parent_id, 'allow_combination', true );
if ( $allow_combination ) {
$parent_product = wc_get_product( $parent_id );
if ( ! is_a( $parent_product, 'WC_Product' ) ) {
return;
}
$variation_title = $parent_product->get_title();
}
if ( $minimum_quantity > 0 && $quantity < $minimum_quantity ) {
if ( $allow_combination && ( $product->is_type( 'variation' ) || $product->is_type( 'variable' ) ) ) {
/* translators: %1$s: Product name, %2$s: Minimum order quantity, %3$s: Total cart quantity */
$notice = sprintf( __( 'To place an order, the quantity of "%1$s" must be at least %2$s. You currently have %3$s in your cart.', 'woocommerce-min-max-quantities' ), $variation_title, $minimum_quantity, $quantity );
throw new Exception( $notice );
} else {
if ( WC_MMQ_Core_Compatibility::is_store_api_request( 'cart' ) ) {
/* translators: %1$s: Product name, %2$s: Minimum order quantity */
$notice = sprintf( __( 'The quantity of "%1$s" has been increased to %2$s. This is the minimum required quantity.', 'woocommerce-min-max-quantities' ), $variation_title, $minimum_quantity );
} else {
/* translators: %1$s: Product name, %2$s: Minimum order quantity */
$notice = sprintf( __( 'To place an order, the quantity of "%1$s" must be at least %2$s.', 'woocommerce-min-max-quantities' ), $variation_title, $minimum_quantity );
}
throw new Exception( $notice );
}
} elseif ( $maximum_quantity > 0 && $quantity > $maximum_quantity ) {
if ( $allow_combination && ( $product->is_type( 'variation' ) || $product->is_type( 'variable' ) ) ) {
/* translators: %1$s: Product name, %2$s: Maximum order quantity, %3$s: Total cart quantity */
$notice = sprintf( __( 'The quantity of "%1$s" cannot be higher than %2$s to place an order. You currently have %3$s in your cart.', 'woocommerce-min-max-quantities' ), $variation_title, $maximum_quantity, $quantity );
throw new Exception( $notice );
} else {
if ( WC_MMQ_Core_Compatibility::is_store_api_request( 'cart' ) ) {
/* translators: %1$s: Product name, %2$s: Maximum order quantity */
$notice = sprintf( __( 'The quantity of "%1$s" has been decreased to %2$s. This is the maximum allowed quantity.', 'woocommerce-min-max-quantities' ), $variation_title, $maximum_quantity );
} else {
/* translators: %1$s: Product name, %2$s: Maximum order quantity */
$notice = sprintf( __( 'The quantity of "%1$s" cannot be higher than %2$s to place an order.', 'woocommerce-min-max-quantities' ), $variation_title, $maximum_quantity );
}
throw new Exception( $notice );
}
}
if ( $group_of_quantity > 0 && ( intval( $quantity ) % intval( $group_of_quantity ) > 0 ) ) {
if ( $allow_combination && ( $product->is_type( 'variation' ) || $product->is_type( 'variable' ) ) ) {
/* translators: %1$s: Product name, %2$d: Group amount */
$notice = sprintf( __( '"%1$s" must be bought in multiples of %2$d. Please adjust its quantity to continue.', 'woocommerce-min-max-quantities' ), $variation_title, $group_of_quantity, $group_of_quantity - ( $quantity % $group_of_quantity ) );
throw new Exception( $notice );
} else {
if ( WC_MMQ_Core_Compatibility::is_store_api_request( 'cart' ) ) {
/* translators: %1$s: Product name, %2$d: Group amount */
$notice = sprintf( __( 'The quantity of "%1$s" has been adjusted. "%1$s" must be bought in multiples of %2$d.', 'woocommerce-min-max-quantities' ), $variation_title, $group_of_quantity, $group_of_quantity - ( $quantity % $group_of_quantity ) );
} else {
/* translators: %1$s: Product name, %2$d: Group amount */
$notice = sprintf( __( '"%1$s" must be bought in multiples of %2$d. Please adjust its quantity to continue.', 'woocommerce-min-max-quantities' ), $variation_title, $group_of_quantity, $group_of_quantity - ( $quantity % $group_of_quantity ) );
}
}
throw new Exception( $notice );
}
} catch ( Exception $e ) {
if ( WC_MMQ_Core_Compatibility::is_store_api_request() ) {
throw $e;
} else {
$notice = $e->getMessage();
if ( $notice ) {
wc_add_notice( $notice, 'error' );
}
}
}
}
/**
* Add to cart validation
*
* @param mixed $pass Filter value.
* @param mixed $product_id Product ID.
* @param mixed $quantity Quantity.
* @param int $variation_id Variation ID (default none).
* @return mixed
*/
public function add_to_cart( $pass, $product_id, $quantity, $variation_id = 0 ) {