I allow users to create their own products using the default commerce product add form (/admin/commerce/products/add/product). Programatically, I need to get the product ID and do more processing.

I have a submit handler attached like this (I use it to redirect users to a custom page):

$form['actions']['submit']['#submit'][] = 'my_module_redirect_handler';

However, I don't seem to have any information about the newly added node. How can I obtain it?


Provided that your additional submit handler is invoked after default one (which by looking how you added it, is the case), you can access nid of the node created as follows:

function my_module_redirect_handler(&$form, &$form_state) {
    $node_id = $form_state['values']['nid'];

Now that you have nid you should be able to do required processing.

Hope that helps.

  • All my fields are there but the nid. – NickO Mar 26 '14 at 16:03
  • thats strange. did you try the same submit on different content type?I just run that very same script on custom content type and it works – sylwester Mar 26 '14 at 16:09
  • I've seen this everywhere on the web for non commerce nodes. I did not change anything so it's a bit confusing. I'm temporarily using the EntityFieldQuery to pull the latest product added but this can be an issue when the activity increases. – NickO Mar 26 '14 at 17:10

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.