Customizing generated coupon codes#
The ShopMagic Customer Coupons add-on provides PHP filters for changing the default coupon prefix, the length of the random part, and the complete generated code. These filters apply to newly generated coupons; they do not rename existing coupons.
Add the snippets to a custom plugin or your child theme's functions.php file. If you paste them inside an existing PHP block, do not add another <?php opening tag. Test your changes on a staging site first.
Change the default coupon prefix#
The default prefix is sm-. Use the following filter to replace it with abc-:
add_filter(
'shopmagic/coupon/default_prefix',
'sm_custom_default_coupon_prefix'
);
function sm_custom_default_coupon_prefix( $prefix ) {
return 'abc-';
}
This changes the default only. If the {{ shop.coupon }} placeholder explicitly supplies a Prefix value, that value overrides the default, including when it is an empty string.
To customize the prefix for one placeholder, use its Prefix parameter instead of a global PHP filter.
Change the coupon code length#
By default, the random part of a generated coupon code contains 10 characters. The prefix is additional: a code with the default sm- prefix therefore contains 13 characters in total.
The following example increases the random part to 20 characters:
add_filter(
'shopmagic/coupon/code_length',
'sm_custom_coupon_length'
);
function sm_custom_coupon_length( $length ) {
return 20;
}
With the default sm- prefix, the resulting code contains 23 characters. Return a positive integer and avoid very short random parts, which increase the chance of generating an existing code.
The default generator uses lowercase letters and numbers while omitting several easily confused characters.
Override the complete coupon code#
The shopmagic/coupon/code filter runs after ShopMagic combines the prefix and random part. It lets you transform or replace the complete code.
This example converts the generated code into a 32-character hexadecimal string:
add_filter(
'shopmagic/coupon/code',
'sm_custom_coupon_code'
);
function sm_custom_coupon_code( $code ) {
return md5( $code );
}
Because this filter replaces the complete code, the original prefix is no longer visible and the final length is 32 characters, regardless of the length configured for the random part.
Keep custom codes unique#
Return a non-empty string suitable for use as a WooCommerce coupon code. ShopMagic checks whether the resulting code already exists and generates another candidate if necessary.
Important: Do not return a fixed code that already exists. ShopMagic will repeatedly retry generation, and a callback that always returns the same existing value can prevent the process from completing.
Filter order and available arguments#
The filters run in this order:
shopmagic/coupon/default_prefixsets the initial prefix. An explicit placeholder prefix can override it.shopmagic/coupon/code_lengthsets the length of the random part.shopmagic/coupon/codereceives the assembled code before the uniqueness check.
The length and complete-code filters also receive the WPDesk\CustomerCoupons\Placeholder\CouponFactory instance as their second argument. To use it in a custom callback, register that callback with two accepted arguments:
add_filter( 'shopmagic/coupon/code', 'your_custom_callback', 10, 2 );
The callback must exist and return the modified code.
Restoring the defaults#
Remove the relevant snippet to restore the default behavior for future coupons. Previously generated coupons retain their existing codes.
