4

I have a fairly basic question I think. I've written a module that uses hook_form_alter to alter the fields in a product form.

But now I'd like to change the fields in the form for a different product and I was thinking of using my custom module to do so.

However, when I use the Devel module to try and find the specific $form_id for each of my created product forms I see they ALL have the SAME name: commerce_checkout_form_checkout. Soooo, I'm not sure how to target a specific form.

Moreover, I've used dpm() to check the variables in the forms and they all have the exact same values for everything. Even across product types. The only value that is different is #build_id but this changes for every form load so it can't be targeted.

Anyone know how to do this?

1
  • Which form are you trying to alter on the checkout form? Commented Feb 8, 2016 at 23:52

3 Answers 3

3

To add or alter the pane forms on the checkout page, for maximum compatibility, you should use the Commerce API.

Define new panes in:

hook_commerce_checkout_pane_info

Alter existing panes:

hook_commerce_checkout_pane_info_alter

For example, you could alter the checkout_form callback for a checkout pane to add your customizations.

checkout_form($form, &$form_state, $checkout_pane, $order): returns form elements for the pane’s checkout form fieldset

You could also add your own custom checkout pane for your custom design there.

You can refer to the existing pane callback to first include or replicate the checkout pane.

Note: I posted a similar answer for adding a custom validate handler to a checkout pane a while ago here: https://drupal.stackexchange.com/a/87061/7153

2

The information you are looking for is available inside the order object of the form_state parameter.

To figure out, from inside a hook_form_alter implementation, which product you are dealing with each time, and assuming that you are only looking at one product per checkout (very weak assumption), the following code would reveal the product:

function raks_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
  $line_item = commerce_line_item_load($form_state['order']->commerce_line_items[LANGUAGE_NONE][0]['line_item_id']);
  $product = commerce_product_load($line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
  if($product->sku == XX && $product->product_id == XX){
    // Alter form accordingly, only for this product
  }
}

N.B.: This is very frail code, it only serves to show how you can identify the product each time you use hook_form_alter. You may need to change the value index for the line item, in the code above I am simply picking the first one.

2
  • This hook_form_alter will error out on altering all forms, not just the checkout form. Better to use the Commerce API for proper compatibility with the Commerce module suite. Note: in the pane checkout_form callback the $order object is passed directly as a parameter. Commented Feb 9, 2016 at 5:53
  • Hey David, I updated my code snippet, to avoid erroring out on altering all forms. Still not going with the option you suggested, but I believe the result is answering the OP. Thanks for the feedback. Commented Feb 9, 2016 at 8:11
1

If you can know in a easy way the form id you can use the Devel form debug module, I use this module in all my projects.

This module adds a contextual link to every form that lets you easily find out form's ID, name of hook_form_FORM_ID_alter() hook and print out form's variables.

Usage:

By hovering over any form you will see a contextual link available. By expanding the link you will see the form ID. Click on the link and you will see a modal window with copyable form id and hook_form_FORM_ID_alter() hook suggestion.

1
  • Well, since I'm using Drupal Commerce this doesn't seem to help. I installed the module and hovered over it and for all of the forms I still get the exact same form_id for all of them: commerce_checkout_form_checkout.
    – Sage
    Commented Feb 7, 2016 at 7:50

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.