-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathCoupon.php
More file actions
129 lines (111 loc) · 2.8 KB
/
Copy pathCoupon.php
File metadata and controls
129 lines (111 loc) · 2.8 KB
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
<?php
/**
* Customer data generation.
*
* @package SmoothGenerator\Classes
*/
namespace WC\SmoothGenerator\Generator;
use WC_Data_Store;
/**
* Customer data generator.
*/
class Coupon extends Generator {
/**
* Create a new coupon.
*
* @param bool $save Whether to save the new coupon to the database.
* @param array $assoc_args Arguments passed via the CLI for additional customization.
*
* @return \WC_Coupon|\WP_Error Coupon object with data populated.
*/
public static function generate( $save = true, $assoc_args = array() ) {
parent::maybe_initialize_generators();
$defaults = array(
'min' => 5,
'max' => 100,
);
list( 'min' => $min, 'max' => $max ) = filter_var_array(
wp_parse_args( $assoc_args, $defaults ),
array(
'min' => array(
'filter' => FILTER_VALIDATE_INT,
'options' => array(
'min_range' => 1,
),
),
'max' => array(
'filter' => FILTER_VALIDATE_INT,
'options' => array(
'min_range' => 1,
),
),
)
);
if ( false === $min ) {
return new \WP_Error(
'smoothgenerator_coupon_invalid_min_max',
'The minimum coupon amount must be a valid positive integer.'
);
}
if ( false === $max ) {
return new \WP_Error(
'smoothgenerator_coupon_invalid_min_max',
'The maximum coupon amount must be a valid positive integer.'
);
}
if ( $min > $max ) {
return new \WP_Error(
'smoothgenerator_coupon_invalid_min_max',
'The maximum coupon amount must be an integer that is greater than or equal to the minimum amount.'
);
}
$code = substr( self::$faker->promotionCode( 1 ), 0, -1 ); // Omit the random digit.
$amount = self::$faker->numberBetween( $min, $max );
$coupon_code = sprintf(
'%s%d',
$code,
$amount
);
$coupon = new \WC_Coupon( $coupon_code );
$coupon->set_props( array(
'code' => $coupon_code,
'amount' => $amount,
) );
if ( $save ) {
$data_store = WC_Data_Store::load( 'coupon' );
$data_store->create( $coupon );
}
/**
* Action: Coupon generator returned a new coupon.
*
* @since 1.2.0
*
* @param \WC_Coupon $coupon
*/
do_action( 'smoothgenerator_coupon_generated', $coupon );
return $coupon;
}
/**
* Create multiple coupons.
*
* @param int $amount The number of coupons to create.
* @param array $args Additional args for coupon creation.
*
* @return int[]|\WP_Error
*/
public static function batch( $amount, array $args = array() ) {
$amount = self::validate_batch_amount( $amount );
if ( is_wp_error( $amount ) ) {
return $amount;
}
$coupon_ids = array();
for ( $i = 1; $i <= $amount; $i ++ ) {
$coupon = self::generate( true, $args );
if ( is_wp_error( $coupon ) ) {
return $coupon;
}
$coupon_ids[] = $coupon->get_id();
}
return $coupon_ids;
}
}