From ff30cc6519a857b69c11f166c018e244c65b4e5f Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Fri, 3 Dec 2021 15:04:45 -0400 Subject: [PATCH 1/4] Add coupon generator and option to orders to add a coupon to each order --- includes/CLI.php | 80 +++++++++++++++++++++++++- includes/GenerateBackgroundProcess.php | 3 + includes/Generator/Coupon.php | 39 +++++++++++++ includes/Generator/Order.php | 10 +++- 4 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 includes/Generator/Coupon.php diff --git a/includes/CLI.php b/includes/CLI.php index 965db21..e77e5ca 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -134,7 +134,8 @@ public static function customers( $args, $assoc_args ) { /** * Disable sending WooCommerce emails when generating objects. */ - protected static function disable_emails() { + protected static function disable_emails() + { $email_actions = array( 'woocommerce_low_stock', 'woocommerce_no_stock', @@ -160,11 +161,57 @@ protected static function disable_emails() { 'woocommerce_created_customer', ); - foreach ( $email_actions as $action ) { - remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 ); + foreach ($email_actions as $action) { + remove_action($action, array('WC_Emails', 'send_transactional_email'), 10, 10); } } + + /** + * Generate coupons. + * + * ## OPTIONS + * + * + * : The amount of coupons to generate + * --- + * default: 100 + * --- + * + * ## EXAMPLES + * wc generate coupons 100 + * + * @param array $args Arguments specified. + * @param array $assoc_args Associative arguments specified. + */ + public static function coupons( $args, $assoc_args ) { + list( $amount ) = $args; + + $amount = (int) $amount; + if ( empty( $amount ) ) { + $amount = 10; + } + + $min = 5; + $max = 100; + if ( ! empty( $assoc_args['min'] ) ) { + $min = $assoc_args['min']; + } + if ( ! empty( $assoc_args['max'] ) ) { + $max = $assoc_args['max']; + } + + if ( $amount > 0 ) { + $progress = \WP_CLI\Utils\make_progress_bar( 'Generating coupons', $amount ); + for ( $i = 1; $i <= $amount; $i++ ) { + Generator\Coupon::generate( true, $min, $max ); + $progress->tick(); + } + $progress->finish(); + } + WP_CLI::success( $amount . ' coupons generated.' ); + } } + WP_CLI::add_command( 'wc generate products', array( 'WC\SmoothGenerator\CLI', 'products' ) ); WP_CLI::add_command( 'wc generate orders', array( 'WC\SmoothGenerator\CLI', 'orders' ), array( 'synopsis' => array( @@ -189,7 +236,34 @@ protected static function disable_emails() { 'type' => 'assoc', 'optional' => true, ), + array( + 'name' => 'coupons', + 'type' => 'assoc', + 'optional' => true, + ), ), ) ); WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) ); +WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array( + 'synopsis' => array( + array( + 'name' => 'amount', + 'type' => 'positional', + 'optional' => true, + 'default' => 10, + ), + array( + 'name' => 'min', + 'optional' => true, + 'type' => 'assoc', + 'default' => 5, + ), + array( + 'name' => 'max', + 'optional' => true, + 'type' => 'assoc', + 'default' => 100, + ), + ), +) ); diff --git a/includes/GenerateBackgroundProcess.php b/includes/GenerateBackgroundProcess.php index 1995ee7..188bba5 100644 --- a/includes/GenerateBackgroundProcess.php +++ b/includes/GenerateBackgroundProcess.php @@ -26,6 +26,9 @@ function wc_smooth_generate_object( $type ) { case 'customer': Generator\Customer::generate(); break; + case 'coupon': + Generator\Coupon::generate(); + break; default: return false; } diff --git a/includes/Generator/Coupon.php b/includes/Generator/Coupon.php new file mode 100644 index 0000000..95c744b --- /dev/null +++ b/includes/Generator/Coupon.php @@ -0,0 +1,39 @@ +set_props( array( + 'code' => "discount$amount", + 'amount' => $amount, + ) ); + $coupon->save(); + + return new \WC_Coupon( $coupon->get_id() ); + } + +} + diff --git a/includes/Generator/Order.php b/includes/Generator/Order.php index 4a1e6a1..dc9be95 100644 --- a/includes/Generator/Order.php +++ b/includes/Generator/Order.php @@ -61,11 +61,17 @@ public static function generate( $save = true, $assoc_args = array() ) { $order->set_status( self::get_status( $assoc_args ) ); $order->calculate_totals( true ); - $date = self::get_date_created( $assoc_args ); + $date = self::get_date_created( $assoc_args ); $date .= ' ' . wp_rand( 0, 23 ) . ':00:00'; $order->set_date_created( $date ); + $include_coupon = ! empty( $assoc_args['status'] ); + if ( $include_coupon ) { + $coupon = Coupon::generate( true ); + $order->apply_coupon( $coupon ); + } + if ( $save ) { $order->save(); } @@ -184,7 +190,7 @@ protected static function get_random_products( int $min_amount = 1, int $max_amo if ( empty( $available_variations ) ) { continue; } - $index = self::$faker->numberBetween( 0, count( $available_variations ) - 1 ); + $index = self::$faker->numberBetween( 0, count( $available_variations ) - 1 ); $products[] = new \WC_Product_Variation( $available_variations[ $index ]['variation_id'] ); } else { $products[] = new \WC_Product( $product_id ); From 89052c17e30a24eaa5923e77ae09a1f95b0ccc80 Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Fri, 3 Dec 2021 15:25:50 -0400 Subject: [PATCH 2/4] Make sure we do not create duplicate coupons --- includes/Generator/Coupon.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/includes/Generator/Coupon.php b/includes/Generator/Coupon.php index 95c744b..0a4ebbc 100644 --- a/includes/Generator/Coupon.php +++ b/includes/Generator/Coupon.php @@ -25,14 +25,17 @@ public static function generate( $save = true, $min = 5, $max = 100 ) { self::init_faker(); $amount = random_int( $min, $max ); - $coupon = new \WC_Coupon(); - $coupon->set_props( array( - 'code' => "discount$amount", - 'amount' => $amount, - ) ); - $coupon->save(); - - return new \WC_Coupon( $coupon->get_id() ); + $coupon_id = "discount$amount"; + $coupon = new \WC_Coupon( $coupon_id ); + if ( $coupon->get_id() === 0 ) { + $coupon->set_props( array( + 'code' => "discount$amount", + 'amount' => $amount, + ) ); + $coupon->save(); + return new \WC_Coupon( $coupon->get_id() ); + } + return $coupon; } } From 4919af55cdcfb1f42668333e7cd698ef3b46c150 Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Mon, 6 Dec 2021 13:37:15 -0400 Subject: [PATCH 3/4] Fix linting --- includes/CLI.php | 7 +++---- includes/Generator/Coupon.php | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/includes/CLI.php b/includes/CLI.php index e77e5ca..5b0ca91 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -134,8 +134,7 @@ public static function customers( $args, $assoc_args ) { /** * Disable sending WooCommerce emails when generating objects. */ - protected static function disable_emails() - { + protected static function disable_emails() { $email_actions = array( 'woocommerce_low_stock', 'woocommerce_no_stock', @@ -161,8 +160,8 @@ protected static function disable_emails() 'woocommerce_created_customer', ); - foreach ($email_actions as $action) { - remove_action($action, array('WC_Emails', 'send_transactional_email'), 10, 10); + foreach ( $email_actions as $action ) { + remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 ); } } diff --git a/includes/Generator/Coupon.php b/includes/Generator/Coupon.php index 0a4ebbc..fa6fa4d 100644 --- a/includes/Generator/Coupon.php +++ b/includes/Generator/Coupon.php @@ -24,9 +24,9 @@ class Coupon extends Generator { public static function generate( $save = true, $min = 5, $max = 100 ) { self::init_faker(); - $amount = random_int( $min, $max ); + $amount = random_int( $min, $max ); $coupon_id = "discount$amount"; - $coupon = new \WC_Coupon( $coupon_id ); + $coupon = new \WC_Coupon( $coupon_id ); if ( $coupon->get_id() === 0 ) { $coupon->set_props( array( 'code' => "discount$amount", From 1d497d44267f79b714d516b1c64e87906eec5389 Mon Sep 17 00:00:00 2001 From: Lourens Schep Date: Thu, 9 Dec 2021 20:14:57 -0400 Subject: [PATCH 4/4] Fix orders CLI param and remove unused faker call --- includes/Generator/Coupon.php | 2 -- includes/Generator/Order.php | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/includes/Generator/Coupon.php b/includes/Generator/Coupon.php index fa6fa4d..bacfc1a 100644 --- a/includes/Generator/Coupon.php +++ b/includes/Generator/Coupon.php @@ -22,8 +22,6 @@ class Coupon extends Generator { * @return \WC_Customer Customer object with data populated. */ public static function generate( $save = true, $min = 5, $max = 100 ) { - self::init_faker(); - $amount = random_int( $min, $max ); $coupon_id = "discount$amount"; $coupon = new \WC_Coupon( $coupon_id ); diff --git a/includes/Generator/Order.php b/includes/Generator/Order.php index dc9be95..176fb03 100644 --- a/includes/Generator/Order.php +++ b/includes/Generator/Order.php @@ -66,7 +66,7 @@ public static function generate( $save = true, $assoc_args = array() ) { $order->set_date_created( $date ); - $include_coupon = ! empty( $assoc_args['status'] ); + $include_coupon = ! empty( $assoc_args['coupons'] ); if ( $include_coupon ) { $coupon = Coupon::generate( true ); $order->apply_coupon( $coupon );