Search code examples
wordpressdynamicwoocommercecoupon

Generate Dynamic couponcode every time in woocommerce


I am using this https://docs.woocommerce.com/document/create-a-coupon-programatically/ code to generate coupon code programmatically.

It's work good. But every time that generate UNIQUECODE same name code. I want to generate different code every time.

Example : UNIQUECODE12, UNIQUECODE14, UNIQUECODE16 etc.

So please help me how this possible.

Thanks.


Solution

  • You could create a WordPress option using the Options API that is a coupon counter. You can access and increment the counter every time you use it.

    <?php
    add_option( 'coupon-count', 0 ); // only sets it if it isn't in the database
    $coupon_count = get_option( 'coupon-count' ); // access count
    $coupon_count++; // increment the count
    update_option( 'coupon-count', $coupon_count ); // store the incremented count for
    $coupon_code = 'UNIQUECODE' . '-' . $coupon_count; // Numbered Code