diff --git a/includes/CLI.php b/includes/CLI.php index 965db21..5b0ca91 100644 --- a/includes/CLI.php +++ b/includes/CLI.php @@ -164,7 +164,53 @@ protected static function disable_emails() { 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 +235,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..bacfc1a --- /dev/null +++ b/includes/Generator/Coupon.php @@ -0,0 +1,40 @@ +get_id() === 0 ) { + $coupon->set_props( array( + 'code' => "discount$amount", + 'amount' => $amount, + ) ); + $coupon->save(); + return new \WC_Coupon( $coupon->get_id() ); + } + return $coupon; + } + +} + diff --git a/includes/Generator/Order.php b/includes/Generator/Order.php index 4a1e6a1..176fb03 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['coupons'] ); + 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 );