How can I react when a commerce product is explicitly created via the UI? how do we add a submit handler in drupal commerce everytime a product is created.

I tried using the following code.

function custommodule_formid_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    $form['#submit'][] = 'custom_submit_function';

    return $form;
}

function custom_submit_function() {
    print "&lt;pre&gt;";
    print_r($form_state);
    print "&lt;/pre&gt;";
    die();
}


Is this the correct way?

I tested it, and actually managed to get the form submit new handler works, but the problem is after the form is submitted, I wanted to get the submitted data and manipulate it. It is not working out as the form data is all gone.

function custommodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {   
    $form['#submit'][] = 'custom_submit_function';
}

function custom_submit_function($form, &amp;$form_state) {
  $value = $form_state['values']['field_data'];
}
If you're only wanting to respond do products created via the UI, then form submit handlers should work. As noted, this wouldn't work for products created by any other method.

The correct hook you should use is hook_commerce_product_insert(). This isn't documented, but it should be. Using this hook will ensure that no matter where the product gets added that the hook runs.

function MYMODULE_commerce_product_insert($product) {
  ...
}


If you're wanting to create 100 new products, you can still loop in this function but you'll just need to differentiate between products that are created and the one that you began looping by some particular method. This could mean, for instance that there is particular SKU format (e.g. all created products end in -XXX where XXX is some number and the one created via the UI does not) or some extra field (e.g. field_parent_product that is empty on the one created in the UI but isn't on the sub-products). It's then trivial to both skip products that have a non-empty field, but also be able to handle updates or deletes down the road.