Created
March 19, 2014 19:12
-
-
Save justinstern/9649063 to your computer and use it in GitHub Desktop.
An example of how to create and use a randomized voucher number like 4ST0P-TGIBC-ZBBGU-VNQ8A with the WooCommerce PDF Product Vouchers plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Create and use a randomized voucher number | |
| function serial_p4u( $number, $voucher ) { | |
| // if we've already generated a custom voucher number, get it and return it | |
| if ( $voucher->get_item_id() ) { | |
| $custom_serial = wc_get_order_item_meta( $voucher->get_item_id(), '_voucher_custom_number', true ); | |
| if ( $custom_serial ) { | |
| return $custom_serial; | |
| } | |
| } | |
| // otherwise, generate one | |
| $tokens = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; | |
| $serial = ''; | |
| for ( $i = 0; $i < 4; $i++ ) { | |
| for ( $j = 0; $j < 5; $j++ ) { | |
| $serial .= $tokens[ rand( 0, 35 ) ]; | |
| } | |
| if ( $i < 3 ) { | |
| $serial .= '-'; | |
| } | |
| } | |
| if ( $voucher->get_item_id() ) { | |
| wc_update_order_item_meta( $voucher->get_item_id(), '_voucher_custom_number', $serial ); | |
| } | |
| return $serial; | |
| } | |
| add_filter('woocommerce_voucher_number', 'serial_p4u', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Justin,
Can you give me some hints at which places I may tweak your code to work with wc 3.x/ wc ppv 3.x
Thank you in advance.
Eric