How do I modify a commerce product before saving or alter the validation?

Specific use case: SKU field of commerce product is required but I want users to be able to leave this blank and have a SKU field auto generated and filled into the field automatically before saving the product.

  • Hi nmc, I guess the first question is, what do you want the SKU generated from? – Stan Ascher Jan 10 '12 at 7:40

Sounds like you want auto sku check out some wonderful code already done http://drupal.org/project/commerce_autosku

Some details from the module's project page:

Commerce AutoSKU allows administrators to generate SKU's for Drupal Commerce products automatically. Using tokens, the SKU can be generated based on the value of arbitrary product fields such as title, author, or any custom fields you have added. Additional sitewide tokens are available when the Token module is enabled.


If you want to alter the validation you can use hook_form_alter to add/alter the validation handlers used for the form.

To change the product you can use one of the entity hooks: hook_entity_insert is probably the one you want to use, but there also is one for update, presave and delete, and they also exist in a variant specific for commerce products: hook_commerce_product_insert


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.