@@ -134,7 +134,8 @@ public static function customers( $args, $assoc_args ) {
134134 /**
135135 * Disable sending WooCommerce emails when generating objects.
136136 */
137- protected static function disable_emails () {
137+ protected static function disable_emails ()
138+ {
138139 $ email_actions = array (
139140 'woocommerce_low_stock ' ,
140141 'woocommerce_no_stock ' ,
@@ -160,11 +161,57 @@ protected static function disable_emails() {
160161 'woocommerce_created_customer ' ,
161162 );
162163
163- foreach ( $ email_actions as $ action ) {
164- remove_action ( $ action , array ( 'WC_Emails ' , 'send_transactional_email ' ), 10 , 10 );
164+ foreach ($ email_actions as $ action ) {
165+ remove_action ($ action , array ('WC_Emails ' , 'send_transactional_email ' ), 10 , 10 );
165166 }
166167 }
168+
169+ /**
170+ * Generate coupons.
171+ *
172+ * ## OPTIONS
173+ *
174+ * <amount>
175+ * : The amount of coupons to generate
176+ * ---
177+ * default: 100
178+ * ---
179+ *
180+ * ## EXAMPLES
181+ * wc generate coupons 100
182+ *
183+ * @param array $args Arguments specified.
184+ * @param array $assoc_args Associative arguments specified.
185+ */
186+ public static function coupons ( $ args , $ assoc_args ) {
187+ list ( $ amount ) = $ args ;
188+
189+ $ amount = (int ) $ amount ;
190+ if ( empty ( $ amount ) ) {
191+ $ amount = 10 ;
192+ }
193+
194+ $ min = 5 ;
195+ $ max = 100 ;
196+ if ( ! empty ( $ assoc_args ['min ' ] ) ) {
197+ $ min = $ assoc_args ['min ' ];
198+ }
199+ if ( ! empty ( $ assoc_args ['max ' ] ) ) {
200+ $ max = $ assoc_args ['max ' ];
201+ }
202+
203+ if ( $ amount > 0 ) {
204+ $ progress = \WP_CLI \Utils \make_progress_bar ( 'Generating coupons ' , $ amount );
205+ for ( $ i = 1 ; $ i <= $ amount ; $ i ++ ) {
206+ Generator \Coupon::generate ( true , $ min , $ max );
207+ $ progress ->tick ();
208+ }
209+ $ progress ->finish ();
210+ }
211+ WP_CLI ::success ( $ amount . ' coupons generated. ' );
212+ }
167213}
214+
168215WP_CLI ::add_command ( 'wc generate products ' , array ( 'WC\SmoothGenerator\CLI ' , 'products ' ) );
169216WP_CLI ::add_command ( 'wc generate orders ' , array ( 'WC\SmoothGenerator\CLI ' , 'orders ' ), array (
170217 'synopsis ' => array (
@@ -189,7 +236,34 @@ protected static function disable_emails() {
189236 'type ' => 'assoc ' ,
190237 'optional ' => true ,
191238 ),
239+ array (
240+ 'name ' => 'coupons ' ,
241+ 'type ' => 'assoc ' ,
242+ 'optional ' => true ,
243+ ),
192244 ),
193245) );
194246WP_CLI ::add_command ( 'wc generate customers ' , array ( 'WC\SmoothGenerator\CLI ' , 'customers ' ) );
195247
248+ WP_CLI ::add_command ( 'wc generate coupons ' , array ( 'WC\SmoothGenerator\CLI ' , 'coupons ' ), array (
249+ 'synopsis ' => array (
250+ array (
251+ 'name ' => 'amount ' ,
252+ 'type ' => 'positional ' ,
253+ 'optional ' => true ,
254+ 'default ' => 10 ,
255+ ),
256+ array (
257+ 'name ' => 'min ' ,
258+ 'optional ' => true ,
259+ 'type ' => 'assoc ' ,
260+ 'default ' => 5 ,
261+ ),
262+ array (
263+ 'name ' => 'max ' ,
264+ 'optional ' => true ,
265+ 'type ' => 'assoc ' ,
266+ 'default ' => 100 ,
267+ ),
268+ ),
269+ ) );
0 commit comments