Coupon_Code_Generator
in package
Generates WooCommerce coupons at email send time for the coupon-code block.
Hooks into the woocommerce_coupon_code_block_auto_generate filter to create a WC_Coupon from block attributes. This provides baseline auto-generation that works without any additional plugins (e.g. MailPoet).
Integrators like MailPoet can hook the same filter at a higher priority to add features like per-subscriber restriction or coupon persistence.
Table of Contents
- MAX_CODE_RETRIES = 5
- Maximum number of retries for generating a unique coupon code.
- generate_coupon() : string
- Generate a WooCommerce coupon from block attributes.
- init() : void
- Initialize the generator by registering the filter hook.
- extract_ids() : array<string|int, mixed>
- Extract integer IDs from an array of {id, title} objects.
- generate_random_code() : string
- Generate a random coupon code in XXXX-XXXXXX-XXXX format.
- generate_unique_code() : string
- Generate a unique random coupon code, retrying on collision.
- parse_email_restrictions() : array<string|int, mixed>
- Parse and validate email restrictions string.
- validate_discount_type() : string
- Validate discount type against WooCommerce's registered types.
Constants
MAX_CODE_RETRIES
Maximum number of retries for generating a unique coupon code.
public
mixed
MAX_CODE_RETRIES
= 5
Methods
generate_coupon()
Generate a WooCommerce coupon from block attributes.
public
generate_coupon(string $coupon_code, array<string|int, mixed> $attrs, Rendering_Context $rendering_context) : string
Parameters
- $coupon_code : string
-
The coupon code (empty if not yet generated).
- $attrs : array<string|int, mixed>
-
Block attributes.
- $rendering_context : Rendering_Context
-
The rendering context.
Return values
string — The generated coupon code, or empty string on failure.init()
Initialize the generator by registering the filter hook.
public
init() : void
Return values
void —extract_ids()
Extract integer IDs from an array of {id, title} objects.
private
extract_ids(array<string|int, mixed> $items) : array<string|int, mixed>
Parameters
- $items : array<string|int, mixed>
-
Array of items with 'id' key.
Return values
array<string|int, mixed> — Array of integer IDs.generate_random_code()
Generate a random coupon code in XXXX-XXXXXX-XXXX format.
private
generate_random_code() : string
Return values
string —generate_unique_code()
Generate a unique random coupon code, retrying on collision.
private
generate_unique_code() : string
Tags
Return values
string — A unique coupon code.parse_email_restrictions()
Parse and validate email restrictions string.
private
parse_email_restrictions(mixed $raw) : array<string|int, mixed>
Parameters
- $raw : mixed
-
Raw email restrictions value (comma-separated string).
Return values
array<string|int, mixed> — Array of valid email addresses.validate_discount_type()
Validate discount type against WooCommerce's registered types.
private
validate_discount_type(string $type) : string
Parameters
- $type : string
-
The discount type to validate.
