Skip to content

Instantly share code, notes, and snippets.

@ScottDeLuzio
Last active April 5, 2021 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottDeLuzio/143fcb124d92b2aa647b72aaba431e27 to your computer and use it in GitHub Desktop.
Save ScottDeLuzio/143fcb124d92b2aa647b72aaba431e27 to your computer and use it in GitHub Desktop.
Filters the discount code generated in Subscriber Discounts for WooCommerce
add_filter( 'sdwoo_discount_code', 'custom_subscriber_discount_code', 10, 2 );
function custom_subscriber_discount_code( $final_code, $email_address ) {
// Start a new discount code.
$new_code = '10OFF-';
// Maximum length of the random string of characters after the new code.
$max_length = 4;
// Choose a random starting point to shorten the string to help avoid repeated strings.
$rand_start = rand( 0, 6 );
// Retrieve x = $max_length characters from $final code using a random starting point (0-6).
$shortened_sting = substr( $final_code, $rand_start, $max_length );
// Combine the new code with the shortened string.
$new_code .= $shortened_string;
return $new_code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment