WooCommerce Code Reference

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

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
stringThe generated coupon code, or empty string on failure.