Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active May 7, 2020 16:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save strangerstudios/6003272 to your computer and use it in GitHub Desktop.
Save strangerstudios/6003272 to your computer and use it in GitHub Desktop.
Change how codes are generated for orders in Paid Memberships Pro. Add this code to your active theme's functions.php or a custom plugin.
/*
Set random order "code" to equal the order ID
It is important to keep the prefix (PMPRO-) below or change it to something else with a non-number in it.
If a code is all numeric, PMPro will go into an infinite loop when a new order is created.
*/
function my_pmpro_random_code($code, $order)
{
global $wpdb;
$prefix = 'PMPRO-';
//already have an id for this order? or should we guess what the next id is going to be?
if(!empty($order->id))
$code = $prefix . $order->id;
else {
//get id of most recent order
$last_order_id = intval($wpdb->get_var('SELECT id FROM $wpdb->pmpro_membership_orders ORDER BY id DESC LIMIT 1'));
//increment and check if it's available until we find one, this is probably goin
$already_used = true;
while($already_used != false) {
$last_order_id++;
$code = $prefix . $last_order_id;
$already_used = $wpdb->get_var("SELECT code FROM $wpdb->pmpro_membership_orders WHERE code = '" . esc_sql($code) . "' LIMIT 1");
}
}
return $code;
}
add_filter("pmpro_random_code", "my_pmpro_random_code", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment