1

I created a marketplace with two fields: a "Store Reference" field that the store owner fills, a "SKU" field I want to be unique and generated automatically.

On Drupal Commerce I want the SKU of my products to be generated automatically and made unique.

I want a sequence of numbers and random letters, for example a single SKU of 12 characters (number and letter).

There is a hash token in "Explore Available Tokens" and "Random". What is it used for?
Can it meet my needs?
Can it generate a unique code that will never be reused?

There are several store owners who can create products at the same time; the same SKU must not be generated.

screenshot

1 Answer 1

3

It's a token for generating a random string. When you look into the implementation, you can see it's the result of hashing 55 random bytes with your selected hashing algorithm (the ? part of random:hash:?).

Will it meet your uniqueness needs? Yes.

Since we're talking pseudo-randomness here and the fact that hashing algorithms can produce collisions, there's a possibility of the same string being generated but it's very unlikely (unless you have a massive number of SKUs being generated).

In the unlikely event that Commerce & the AutoSKU don't have the logic to prevent SKU collision (which should be there as its a unique attribute), you can always add more patterns in the token (e.g. UID) to lessen the collision possibility to effectively zero, for the number of SKUs needed by your store.

2
  • Thank you. I have to select what like hash to generate numbers and letters. How to choose the length. I would like to have 12 and 20 characters.
    – 1ad9ac48
    Feb 25, 2018 at 21:31
  • 1
    Those aren't parameters in the token. If you want more than just a unique string, you'll probably need to implement your own token that can limit the string length & and used characters.
    – Shawn Conn
    Feb 26, 2018 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.