Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
161c802
Add --coupon-ratio parameter to order generator
layoutd Oct 22, 2025
df0fda0
Add --refund-ratio parameter to order generator
layoutd Oct 22, 2025
4d3eb9d
Improve refund logic to properly handle line items and fees
layoutd Oct 22, 2025
4dd4a96
Fix refund tax calculation to properly handle tax rate IDs
layoutd Oct 22, 2025
84a81bc
Calculate explicit refund amount from line items
layoutd Oct 22, 2025
368e5b7
Fix refund ratio logic and add multiple refund support
layoutd Oct 22, 2025
479a1a8
Update partial refund reason to show products and items
layoutd Oct 22, 2025
4f98a67
Force partial refunds for orders with existing refunds
layoutd Oct 24, 2025
a3e1536
Recalculate order totals after applying coupon
layoutd Oct 24, 2025
74ae8e1
Update includes/Generator/Order.php
layoutd Oct 24, 2025
e2494ec
Doc update
layoutd Oct 24, 2025
ff277e2
Fix array_rand edge case for orders with exactly 2 items
layoutd Oct 24, 2025
f80b01e
Refactor coupon creation to use Coupon::generate()
layoutd Oct 24, 2025
ee9ff32
Move coupon retrieval logic to Coupon::get_random()
layoutd Oct 24, 2025
07daacb
Use WordPress get_posts() API instead of raw SQL queries
layoutd Oct 24, 2025
57daab8
Add discount_type parameter to Coupon generator
layoutd Oct 24, 2025
24b2493
Refactor Order generator to use Coupon::batch()
layoutd Oct 24, 2025
f8d718c
Add discount_type parameter to CLI coupon command
layoutd Oct 24, 2025
080ee99
Update README with new coupon and order parameters
layoutd Oct 24, 2025
b9c85f9
Clarify that refunds are split evenly between partial and full
layoutd Oct 24, 2025
86a499c
Fix backwards compatibility: only set discount_type when explicitly p…
layoutd Oct 24, 2025
2c9940e
Add input validation for coupon-ratio and refund-ratio parameters
layoutd Oct 24, 2025
5b53925
Add performance comment for get_posts() in Coupon::get_random()
layoutd Oct 24, 2025
0816747
Clarify that --coupons flag is equivalent to --coupon-ratio=1.0
layoutd Oct 24, 2025
0f5dd77
Fix ratio probability calculation using integer-based random generation
layoutd Oct 24, 2025
f69d126
Add check to prevent refunds with empty line items
layoutd Oct 24, 2025
73cdb1b
Improve refund error handling and logging
layoutd Oct 24, 2025
ae9b6a6
Add validation to prevent refunds with invalid amounts
layoutd Oct 24, 2025
ac5f820
Fix refund amount validation to prevent exceeding order total
layoutd Oct 24, 2025
0b3faa5
Fix refund amount calculations to prevent rounding errors and ensure …
layoutd Oct 24, 2025
9894e7e
Fix partial refunds to track already-refunded quantities and prevent …
layoutd Oct 24, 2025
c6d22b4
Fix code quality issues in refund calculations
layoutd Oct 24, 2025
323a1a7
Add realistic date handling for refunds
layoutd Oct 24, 2025
5332818
Optimize batch order generation with ID pre-fetching
layoutd Oct 24, 2025
5734f42
Refactor refund generation: extract constants and helper methods
layoutd Oct 27, 2025
18a480c
Add safety check before array_rand to prevent errors with empty arrays
layoutd Oct 27, 2025
f08714c
Make default 'fixed_cart' Coupon generator explicit
layoutd Oct 28, 2025
20f415b
Clarify memory impact
layoutd Oct 28, 2025
553d847
Clarify that decimal ratios for coupons and refunds are converted to …
layoutd Oct 28, 2025
e17884b
Add error logging for order generation and coupon application failures
layoutd Oct 28, 2025
daad864
Merge remote-tracking branch 'origin/add-order-coupon-refund-ratios' …
layoutd Oct 30, 2025
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ Generate orders with random dates between `--date-start` and `--date-end`.
Generate orders with a specific status.
- `wp wc generate orders <nr of orders> --status=completed`

Apply coupons to a percentage of generated orders (0.0-1.0). If no coupons exist, 6 will be created automatically (3 fixed cart, 3 percentage). Note: `--coupons` flag is equivalent to `--coupon-ratio=1.0`.

**Important:** Decimal ratios are converted to percentages using integer rounding. For example, `0.505` becomes 50% (not 50.5%) because the random generation uses integer comparison. Use whole percentages like `0.50` for precise 50% ratios.
- `wp wc generate orders <nr of orders> --coupon-ratio=0.5`

Refund a percentage of completed orders (0.0-1.0). Refunds will be split evenly between partial and full, and 25% of partial refunds will receive a second partial refund.

**Note:** The same decimal ratio behavior applies to refund ratios as described above for coupon ratios.
- `wp wc generate orders <nr of orders> --status=completed --refund-ratio=0.3`

#### Order Attribution

Order Attribution represents the origin of data for an order. By default, random values are generated and assigned to the order. Orders with a creation date before 2024-01-09 will not have attribution metadata added, as the feature was not available in WooCommerce at that time.
Expand All @@ -52,6 +62,9 @@ Generate coupons with a minimum discount amount.
Generate coupons with a maximum discount amount.
- `wp wc generate coupons <nr of coupons> --max=50`

Generate coupons with a specific discount type. Options are `fixed_cart` or `percent`. If not specified, defaults to WooCommerce default (fixed_cart).
- `wp wc generate coupons <nr of coupons> --discount_type=percent --min=5 --max=25`

### Customers

Generate customers based on the number of customers parameter.
Expand Down
23 changes: 21 additions & 2 deletions includes/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,19 @@ function () use ( $progress ) {
array(
'name' => 'coupons',
'type' => 'flag',
'description' => 'Create and apply a coupon to each generated order.',
'description' => 'Create and apply a coupon to each generated order. Equivalent to --coupon-ratio=1.0.',
'optional' => true,
),
array(
'name' => 'coupon-ratio',
'type' => 'assoc',
'description' => 'Decimal ratio (0.0-1.0) of orders that should have coupons applied. If no coupons exist, 6 will be created (3 fixed value, 3 percentage). Note: Decimal values are converted to percentages using integer rounding (e.g., 0.505 becomes 50%).',
'optional' => true,
),
array(
'name' => 'refund-ratio',
'type' => 'assoc',
'description' => 'Decimal ratio (0.0-1.0) of completed orders that should be refunded (wholly or partially). Note: Decimal values are converted to percentages using integer rounding (e.g., 0.505 becomes 50%).',
'optional' => true,
),
array(
Expand Down Expand Up @@ -381,8 +393,15 @@ function () use ( $progress ) {
'optional' => true,
'default' => 100,
),
array(
'name' => 'discount_type',
'type' => 'assoc',
'description' => 'The type of discount for the coupon. If not specified, defaults to WooCommerce default (fixed_cart).',
'optional' => true,
'options' => array( 'fixed_cart', 'percent' ),
),
),
'longdesc' => "## EXAMPLES\n\nwc generate coupons 10\n\nwc generate coupons 50 --min=1 --max=50",
'longdesc' => "## EXAMPLES\n\nwc generate coupons 10\n\nwc generate coupons 50 --min=1 --max=50\n\nwc generate coupons 20 --discount_type=percent --min=5 --max=25",
) );

WP_CLI::add_command( 'wc generate terms', array( 'WC\SmoothGenerator\CLI', 'terms' ), array(
Expand Down
69 changes: 61 additions & 8 deletions includes/Generator/Coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ class Coupon extends Generator {
public static function generate( $save = true, $assoc_args = array() ) {
parent::maybe_initialize_generators();

$defaults = array(
'min' => 5,
'max' => 100,
);
$defaults = array(
'min' => 5,
'max' => 100,
'discount_type' => 'fixed_cart',
);

$args = wp_parse_args( $assoc_args, $defaults );

list( 'min' => $min, 'max' => $max ) = filter_var_array(
wp_parse_args( $assoc_args, $defaults ),
$args,
array(
'min' => array(
'filter' => FILTER_VALIDATE_INT,
Expand Down Expand Up @@ -68,6 +71,15 @@ public static function generate( $save = true, $assoc_args = array() ) {
);
}

// Validate discount_type if provided
$discount_type = ! empty( $args['discount_type'] ) ? $args['discount_type'] : '';
if ( ! empty( $discount_type ) && ! in_array( $discount_type, array( 'fixed_cart', 'percent' ), true ) ) {
return new \WP_Error(
'smoothgenerator_coupon_invalid_discount_type',
'The discount_type must be either "fixed_cart" or "percent".'
);
}

$code = substr( self::$faker->promotionCode( 1 ), 0, -1 ); // Omit the random digit.
$amount = self::$faker->numberBetween( $min, $max );
$coupon_code = sprintf(
Expand All @@ -76,11 +88,18 @@ public static function generate( $save = true, $assoc_args = array() ) {
$amount
);

$coupon = new \WC_Coupon( $coupon_code );
$coupon->set_props( array(
$props = array(
'code' => $coupon_code,
'amount' => $amount,
) );
);

// Only set discount_type if explicitly provided
if ( ! empty( $discount_type ) ) {
$props['discount_type'] = $discount_type;
}

$coupon = new \WC_Coupon( $coupon_code );
$coupon->set_props( $props );

if ( $save ) {
$data_store = WC_Data_Store::load( 'coupon' );
Expand Down Expand Up @@ -125,5 +144,39 @@ public static function batch( $amount, array $args = array() ) {

return $coupon_ids;
}

/**
* Get a random existing coupon.
*
* @param array|null $cached_coupon_ids Optional array of coupon IDs to use instead of querying.
* @return \WC_Coupon|false Coupon object or false if none available.
*/
public static function get_random( $cached_coupon_ids = null ) {
// Use cached IDs if provided (batch mode optimization)
if ( null !== $cached_coupon_ids && ! empty( $cached_coupon_ids ) ) {
$coupon_ids = $cached_coupon_ids;
} else {
// Fallback to querying for coupon IDs
// Note: Using posts_per_page=-1 loads all coupons into memory for random selection.
// For stores with thousands of coupons, consider using direct SQL with RAND() for better performance.
// This approach was chosen for consistency with WordPress APIs and to avoid raw SQL queries.
$coupon_ids = get_posts(
array(
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
)
);
}

if ( empty( $coupon_ids ) ) {
return false;
}

$random_coupon_id = $coupon_ids[ array_rand( $coupon_ids ) ];

return new \WC_Coupon( $random_coupon_id );
}
}

Loading