I am building a Drupal Commerce site and I had the weirdest request:

Upon creating a new product, the client want's to select the product's category from a select list or something similar. This should result in the first part of the product's SKU, say


Then he want's to select another attribute for that specific product which produces the second part of the SKU, say


So the complete SKU of the product would be CAT007.048 thus resulting in a unique identifier. The reason for this is that he doesn't want to look within huge arrays in order to produce the correct SKU.

My problem is that I can't seem to find a decent solution with which I can give the client access and management over the arrays.

Can anyone come up with something?

EDIT something that came to mind was taxonomy terms that contain a field with their SKU prefix/suffix. The problem that arises from that is how can I pass the tid from the user's selection (javascript) to the php side in order to do a term_load() and get the field's value.


By using taxonomy_vocabulary_machine_name_load() and taxonomy_get_tree() I was able to extract the 2 taxonomies I needed for creating the prefix and suffix of the SKU.

I created tables of key, value pairs that contained the tids and the SKU codes.

By using json_encode() I was able to pass those tables into an inline js script I attached to commerce's product form using $form['#attached']['js'].

Now by having a JavaScript array mapping tids to SKU parts, it's easy using jQuery to fill in the SKU textfield accordingly. :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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