Skip to content

Commit bb1165d

Browse files
authored
Merge pull request #71 from woocommerce/add-coupons-generator
Add coupon generator and option to orders to add a coupon to each order
2 parents 1601e2f + 1d497d4 commit bb1165d

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

includes/CLI.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,53 @@ protected static function disable_emails() {
164164
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
165165
}
166166
}
167+
168+
/**
169+
* Generate coupons.
170+
*
171+
* ## OPTIONS
172+
*
173+
* <amount>
174+
* : The amount of coupons to generate
175+
* ---
176+
* default: 100
177+
* ---
178+
*
179+
* ## EXAMPLES
180+
* wc generate coupons 100
181+
*
182+
* @param array $args Arguments specified.
183+
* @param array $assoc_args Associative arguments specified.
184+
*/
185+
public static function coupons( $args, $assoc_args ) {
186+
list( $amount ) = $args;
187+
188+
$amount = (int) $amount;
189+
if ( empty( $amount ) ) {
190+
$amount = 10;
191+
}
192+
193+
$min = 5;
194+
$max = 100;
195+
if ( ! empty( $assoc_args['min'] ) ) {
196+
$min = $assoc_args['min'];
197+
}
198+
if ( ! empty( $assoc_args['max'] ) ) {
199+
$max = $assoc_args['max'];
200+
}
201+
202+
if ( $amount > 0 ) {
203+
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating coupons', $amount );
204+
for ( $i = 1; $i <= $amount; $i++ ) {
205+
Generator\Coupon::generate( true, $min, $max );
206+
$progress->tick();
207+
}
208+
$progress->finish();
209+
}
210+
WP_CLI::success( $amount . ' coupons generated.' );
211+
}
167212
}
213+
168214
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ) );
169215
WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array(
170216
'synopsis' => array(
@@ -189,7 +235,34 @@ protected static function disable_emails() {
189235
'type' => 'assoc',
190236
'optional' => true,
191237
),
238+
array(
239+
'name' => 'coupons',
240+
'type' => 'assoc',
241+
'optional' => true,
242+
),
192243
),
193244
) );
194245
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );
195246

247+
WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array(
248+
'synopsis' => array(
249+
array(
250+
'name' => 'amount',
251+
'type' => 'positional',
252+
'optional' => true,
253+
'default' => 10,
254+
),
255+
array(
256+
'name' => 'min',
257+
'optional' => true,
258+
'type' => 'assoc',
259+
'default' => 5,
260+
),
261+
array(
262+
'name' => 'max',
263+
'optional' => true,
264+
'type' => 'assoc',
265+
'default' => 100,
266+
),
267+
),
268+
) );

includes/GenerateBackgroundProcess.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function wc_smooth_generate_object( $type ) {
2626
case 'customer':
2727
Generator\Customer::generate();
2828
break;
29+
case 'coupon':
30+
Generator\Coupon::generate();
31+
break;
2932
default:
3033
return false;
3134
}

includes/Generator/Coupon.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Customer data generation.
4+
*
5+
* @package SmoothGenerator\Classes
6+
*/
7+
8+
namespace WC\SmoothGenerator\Generator;
9+
10+
/**
11+
* Customer data generator.
12+
*/
13+
class Coupon extends Generator {
14+
15+
16+
/**
17+
* Return a new customer.
18+
*
19+
* @param bool $save Save the object before returning or not.
20+
* @param int $min minimum coupon amount.
21+
* @param int $max maximum coupon amount.
22+
* @return \WC_Customer Customer object with data populated.
23+
*/
24+
public static function generate( $save = true, $min = 5, $max = 100 ) {
25+
$amount = random_int( $min, $max );
26+
$coupon_id = "discount$amount";
27+
$coupon = new \WC_Coupon( $coupon_id );
28+
if ( $coupon->get_id() === 0 ) {
29+
$coupon->set_props( array(
30+
'code' => "discount$amount",
31+
'amount' => $amount,
32+
) );
33+
$coupon->save();
34+
return new \WC_Coupon( $coupon->get_id() );
35+
}
36+
return $coupon;
37+
}
38+
39+
}
40+

includes/Generator/Order.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ public static function generate( $save = true, $assoc_args = array() ) {
6161
$order->set_status( self::get_status( $assoc_args ) );
6262
$order->calculate_totals( true );
6363

64-
$date = self::get_date_created( $assoc_args );
64+
$date = self::get_date_created( $assoc_args );
6565
$date .= ' ' . wp_rand( 0, 23 ) . ':00:00';
6666

6767
$order->set_date_created( $date );
6868

69+
$include_coupon = ! empty( $assoc_args['coupons'] );
70+
if ( $include_coupon ) {
71+
$coupon = Coupon::generate( true );
72+
$order->apply_coupon( $coupon );
73+
}
74+
6975
if ( $save ) {
7076
$order->save();
7177
}
@@ -184,7 +190,7 @@ protected static function get_random_products( int $min_amount = 1, int $max_amo
184190
if ( empty( $available_variations ) ) {
185191
continue;
186192
}
187-
$index = self::$faker->numberBetween( 0, count( $available_variations ) - 1 );
193+
$index = self::$faker->numberBetween( 0, count( $available_variations ) - 1 );
188194
$products[] = new \WC_Product_Variation( $available_variations[ $index ]['variation_id'] );
189195
} else {
190196
$products[] = new \WC_Product( $product_id );

0 commit comments

Comments
 (0)