• Resolved geertvanderheide

    (@geertvanderheide)


    (I answered my own question – see reply).

    I last used this plugin in 2022, and I added the following code to successfully customize the coupon format:

    // Set desired coupon generation format
    function set_coupon_format($random_coupon) {
    $length = 6;
    $charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $count = strlen( $charset );

    while ( $length-- ) {
    $random_coupon .= $charset[ mt_rand( 0, $count-1 ) ];
    }

    return $random_coupon;
    }
    add_filter('woocommerce_coupon_generator_random_coupon_code', 'set_coupon_format', 10, 1);

    This worked well before, but now when I generate new coupons they come out with the standard format instead of the one I want. The code above is still there, in functions.php of my active theme. I believe I’m using the correct hook. But it’s not being used somehow.

    Any tips as to how this is happening and how I fix it?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter geertvanderheide

    (@geertvanderheide)

    Ah, I see the issue now. My custom code added extra characters to the existing coupon code instead of re-generating the codes entirely. My problem was solved by changing the code to the following:

    // Set desired coupon generation format
    function set_coupon_format($random_coupon) {
    $length = 6;
    $charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
    $count = strlen( $charset );
    $random_coupon = '';

    while ( $length-- ) {
    $random_coupon .= $charset[ mt_rand( 0, $count-1 ) ];
    }

    return $random_coupon;
    }
    add_filter('woocommerce_coupon_generator_random_coupon_code', 'set_coupon_format', 10, 1);

    For anyone else looking to customize the coupon format: the above should be added to functions.php of the active theme. The length variable sets the number of characters for the generated codes, and the charset variable decides which characters are to be used (I left out characters that can be confusing to read like I, 1, O and 0).

    @geertvanderheide I created account only to thank you for this piece of code, it works brilliantly. Big thanks to the author of the plugin too, it’s simple and does the job well.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom code format no longer working?’ is closed to new replies.