wc-smooth-generator icon indicating copy to clipboard operation
wc-smooth-generator copied to clipboard

Coupons: Increase uniqueness of coupon codes

Open coreymckrill opened this issue 2 years ago • 0 comments

Currently generated coupon codes take the form of "discount$amount". This means that given a small enough range of discount values and/or a large enough number of coupons to generate, you will end up generating multiple coupons with the same code. When that happens, the duplicate is skipped, but it still counts towards the total number that the CLI script generates.

One way to solve this would be to replace discount in the coupon code with a string from the faker library we're already using. In fact, the mbezhanov/faker-provider-collection library we use in the Product generator has a promotionCode method that might be perfect for this:

self::$faker->addProvider( new \Bezhanov\Faker\Provider\Commerce( self::$faker ) );

$coupon_id = sprintf(
	%s%d,
	self::$faker->promotionCode( 0 ), // We don't want random digits, we want the actual amount of the discount.
	$amount
);

coreymckrill avatar Mar 13 '23 19:03 coreymckrill