Skip to content

Commit 86a499c

Browse files
committed
Fix backwards compatibility: only set discount_type when explicitly provided
1 parent b9c85f9 commit 86a499c

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Generate coupons with a minimum discount amount.
5858
Generate coupons with a maximum discount amount.
5959
- `wp wc generate coupons <nr of coupons> --max=50`
6060

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

6464
### Customers

includes/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function () use ( $progress ) {
396396
array(
397397
'name' => 'discount_type',
398398
'type' => 'assoc',
399-
'description' => 'The type of discount for the coupon. If not specified, a random type will be chosen.',
399+
'description' => 'The type of discount for the coupon. If not specified, defaults to WooCommerce default (fixed_cart).',
400400
'optional' => true,
401401
'options' => array( 'fixed_cart', 'percent' ),
402402
),

includes/Generator/Coupon.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ public static function generate( $save = true, $assoc_args = array() ) {
8080
);
8181
}
8282

83-
// If no discount type specified, randomly choose one for backwards compatibility
84-
if ( empty( $discount_type ) ) {
85-
$discount_type = wp_rand( 0, 1 ) === 0 ? 'fixed_cart' : 'percent';
86-
}
87-
8883
$code = substr( self::$faker->promotionCode( 1 ), 0, -1 ); // Omit the random digit.
8984
$amount = self::$faker->numberBetween( $min, $max );
9085
$coupon_code = sprintf(
@@ -93,12 +88,18 @@ public static function generate( $save = true, $assoc_args = array() ) {
9388
$amount
9489
);
9590

91+
$props = array(
92+
'code' => $coupon_code,
93+
'amount' => $amount,
94+
);
95+
96+
// Only set discount_type if explicitly provided
97+
if ( ! empty( $discount_type ) ) {
98+
$props['discount_type'] = $discount_type;
99+
}
100+
96101
$coupon = new \WC_Coupon( $coupon_code );
97-
$coupon->set_props( array(
98-
'code' => $coupon_code,
99-
'amount' => $amount,
100-
'discount_type' => $discount_type,
101-
) );
102+
$coupon->set_props( $props );
102103

103104
if ( $save ) {
104105
$data_store = WC_Data_Store::load( 'coupon' );

0 commit comments

Comments
 (0)