• Resolved beamkiller

    (@beamkiller)


    Hi,

    I have succesfully write a small snippet to generate coupon codes from an array. Everything works except, the title of the coupon codes.

    My snippet, which is from WooCommerce pages.

    So this will generate two coupons, but the title has no leading zeros as my array elements. See this.

    I have also tried woocommerce_coupon_code hook.

    /*-----------------------------------------------------------------------------------*/
    /* Add Leading Zeros before coupon codes generating */
    /*-----------------------------------------------------------------------------------*/
    function zerofill ($num, $zerofill = 8)
    {
    	return str_pad($num, $zerofill, '0', STR_PAD_LEFT);
    }
    
    // add the filter 
    add_filter( 'woocommerce_coupon_code', 'filter_woocommerce_coupon_code', 10, 1 );
    
    // define the woocommerce_coupon_code callback 
    function filter_woocommerce_coupon_code( $post_post_title )
    {
    	//error_log("COUPON: ".$post_post_title);
    	return zerofill($post_post_title);
    }

    https://ww.wp.xz.cn/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Mike Jolley

    (@mikejolley)

    I think it’s because you’re using integers and not strings. Integers cannot have leading zero.

    Thread Starter beamkiller

    (@beamkiller)

    Ah, you were right!

    Thanks Mike! 🙂

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

The topic ‘Generating coupon codes programatically’ is closed to new replies.