Skip to content

Commit ff30cc6

Browse files
committed
Add coupon generator and option to orders to add a coupon to each order
1 parent 1601e2f commit ff30cc6

File tree

4 files changed

+127
-5
lines changed

4 files changed

+127
-5
lines changed

includes/CLI.php

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
168215
WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ) );
169216
WP_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
) );
194246
WP_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+
) );

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
self::init_faker();
26+
27+
$amount = random_int( $min, $max );
28+
$coupon = new \WC_Coupon();
29+
$coupon->set_props( array(
30+
'code' => "discount$amount",
31+
'amount' => $amount,
32+
) );
33+
$coupon->save();
34+
35+
return new \WC_Coupon( $coupon->get_id() );
36+
}
37+
38+
}
39+

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['status'] );
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)