Skip to content

Commit 89052c1

Browse files
committed
Make sure we do not create duplicate coupons
1 parent ff30cc6 commit 89052c1

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

includes/Generator/Coupon.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ public static function generate( $save = true, $min = 5, $max = 100 ) {
2525
self::init_faker();
2626

2727
$amount = random_int( $min, $max );
28-
$coupon = new \WC_Coupon();
29-
$coupon->set_props( array(
30-
'code' => "discount$amount",
31-
'amount' => $amount,
32-
) );
33-
$coupon->save();
34-
35-
return new \WC_Coupon( $coupon->get_id() );
28+
$coupon_id = "discount$amount";
29+
$coupon = new \WC_Coupon( $coupon_id );
30+
if ( $coupon->get_id() === 0 ) {
31+
$coupon->set_props( array(
32+
'code' => "discount$amount",
33+
'amount' => $amount,
34+
) );
35+
$coupon->save();
36+
return new \WC_Coupon( $coupon->get_id() );
37+
}
38+
return $coupon;
3639
}
3740

3841
}

0 commit comments

Comments
 (0)