1

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(&$form, &$form_state, $form_id) {
    $form['#submit'][] = 'custom_submit_function';

    return $form;
}

function custom_submit_function() {
    print "<pre>";
    print_r($form_state);
    print "</pre>";
    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(&$form, &$form_state, $form_id) {   
    $form['#submit'][] = 'custom_submit_function';
}

function custom_submit_function($form, &$form_state) {
  $value = $form_state['values']['field_data'];
}
3
  • Are you only wanting this to occur when a product is created via the Products UI? Because the Products UI uses forms and would apply to what you are saying, however, that is not the only way that a product can be created.
    – nvahalik
    Commented Jan 18, 2016 at 15:41
  • I wanted to catch when one product is created, then generate the same products but with different SKU only that is why I used this particulare method
    – Boby
    Commented Jan 18, 2016 at 15:57
  • Then you should update your question to include that specific detail.
    – nvahalik
    Commented Jan 18, 2016 at 16:39

3 Answers 3

2

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.

5
  • I wanted to generate 100 nodes upon product insert, so I think this one will be looping if I do that Nvahalik
    – Boby
    Commented Jan 18, 2016 at 15:01
  • This only reacts upon a product entity insert. If you're creating nodes then this hook will not fire.
    – nvahalik
    Commented Jan 18, 2016 at 15:02
  • I am going to generate 100 products sorry typo, node will be fine yes I tested it :) products that loop and throws out error so I am looking how to solve this in the quickest way
    – Boby
    Commented Jan 18, 2016 at 15:09
  • You'd have to provide more detail has to why this is the case. Normally if you're generating products based on some action, something would differ between the original entity and what you're creating (e.g. creating products in response to a node insert or creating a product of a different type). In any case, you should be able to test for some condition (e.g. a type of product) and just return if it is the type you're creating in the loop.
    – nvahalik
    Commented Jan 18, 2016 at 15:27
  • I am trying to generate 100 products based on one entity, but only different in SKU, the rest will be the same..
    – Boby
    Commented Jan 18, 2016 at 15:36
0

If you want to replace the existing submit handler then you need to remove the [] this: Something like this

$form['#submit'] = 'custom_submit_function';

Also be sure to check the form id of your form and the weight of your module.

2
  • I still want the original function to work, and add a new function into it
    – Boby
    Commented Jan 18, 2016 at 15:02
  • then your function will add another submit handler. So first your submit handler will fired followed by module own submit handler
    – Shabir A.
    Commented Jan 18, 2016 at 15:07
0

Yes, this is correct way. But it will work only if product is created with the help of form. If you want to add some actions every time when product is created even it is created without the help of product form(e.g programmatically) you should use hook_entity_insert

UPD: You had to say about autogenerated products in your initial post. You may add some property to all products that are created there and check it in hook_entity_insert. Example(maybe need some modifications):

/**
 * Implements hook_entity_insert().
 */
function mymodule_entity_insert($entity, $type) {
  if ($type == 'product') {
    if (empty($entity->auto_generate)) {
      $new_product = entity_create('product');
      $new_product->auto_generated = TRUE;
      $new_product->save();
    }
  }
}
2
  • I tried using hook_entity_insert then punch auto generate 100 products after one particular entity is saved. but the major problems are its looping inside the hook_entity_insert if I add the 100 products auto generate. so I can not use entity_insert for this matter
    – Boby
    Commented Jan 18, 2016 at 15:04
  • let me give it a try maria and let you know thank you!
    – Boby
    Commented Jan 18, 2016 at 16:00

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.