Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save justinstern/9649063 to your computer and use it in GitHub Desktop.

Select an option

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
<?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);
@epoatgh
Copy link

epoatgh commented Aug 11, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment