lundi 27 juin 2022

Generate a randon order number but prevent regeneration in WooCommerce

I am trying to add a random string when the order number is created as the default sequential number can be very easily guessed.

I tried this snippet:

function generate_random_string( $length = 16 ) {
    return substr( str_shuffle( str_repeat( $x = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil( $length / strlen( $x ) ) ) ), 1, $length );
}
    
add_filter( 'woocommerce_order_number', 'ct_change_woocommerce_order_number', 1, 2);

function ct_change_woocommerce_order_number( $order_id, $order ) {
     $random_string1 = generate_random_string(5);
    return $random_string1 . $order->get_id();
}

The problem is that this change the order number every time the order number is requested somewhere.

This would work if I will use a constant prefix and suffix, but in the actual way a different order number is shown each time for the same order. Any advice?




Aucun commentaire:

Enregistrer un commentaire