Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions includes/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,53 @@ protected static function disable_emails() {
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
}
}

/**
* Generate coupons.
*
* ## OPTIONS
*
* <amount>
* : 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(
Expand All @@ -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,
),
),
) );
3 changes: 3 additions & 0 deletions includes/GenerateBackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
40 changes: 40 additions & 0 deletions includes/Generator/Coupon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Customer data generation.
*
* @package SmoothGenerator\Classes
*/

namespace WC\SmoothGenerator\Generator;

/**
* Customer data generator.
*/
class Coupon extends Generator {


/**
* Return a new customer.
*
* @param bool $save Save the object before returning or not.
* @param int $min minimum coupon amount.
* @param int $max maximum coupon amount.
* @return \WC_Customer Customer object with data populated.
*/
public static function generate( $save = true, $min = 5, $max = 100 ) {
$amount = random_int( $min, $max );
$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;
}

}

10 changes: 8 additions & 2 deletions includes/Generator/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 );
Expand Down