Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I am not seeing my results from my code on trying to alter form values for the Shipping/Billing default_values. I have a two-step checkout process in which the first checkout page/pane (a custom addition in my module) captures 5 important customer fields that I want to be able to re-use in the Shipping/Billing form. I deposit this information into $order->data and pass it into the $form upon submission. I can output the changes to the form in devel but the data is not being displayed on the form itself under the Shipping/Billing fields.
This is the code I'm using presently:
function my_module_form_alter(&$form, $form_state, $form_id) {
if($form_id == "commerce_checkout_form_checkout"){
// $form['cart_contents']['#title'] = "My Deals";
$checkout_page = commerce_checkout_pages();
$checkout_page = $checkout_page['checkout'];
foreach (commerce_checkout_panes(array('enabled' => TRUE, 'page' => $checkout_page['page_id'])) as $pane_id => $checkout_pane) {
if ($callback = commerce_checkout_pane_callback($checkout_pane, 'checkout_form')) {
// Generate the pane form.
$pane_form = $callback($form, $form_state, $checkout_pane, $form_state['order']);
dpm($form);
$holder = $form_state['order']->data;
$name = $holder['ShippingFirstName'] . ' ' . $holder['ShippingLastName'];
$form_state->values['customer_profile_shipping']['commerce_customer_address']['und'][0] = array(
'name_line' => $name,
);
}
}
}
}
Comments
Comment #1
generalconsensus CreditAttribution: generalconsensus commentedI would love to hear from the community on this.
Comment #2
marcin.wosinek CreditAttribution: marcin.wosinek commentedCulprit for not seeing changes will be the first line:
function my_module_form_alter(&$form, $form_state, $form_id) {
You are missing:
hook_form_alter(&$form, <strong>&</strong>$form_state, $form_id)
When you have & before parameter it's used by reference - so all changes done locally in function are done in original object. Without it, you are just changing local copy of
$form_state
.Comment #3
generalconsensus CreditAttribution: generalconsensus commentedwow that was a pretty silly mistake, glad you noticed that. Sadly I still haven't been able to push new values to items like:
$form_state['field']['#parents']['customer_profile_shipping']['#fields']['commerce_customer_address']['und']['field']['columns']['name_line']['default'] = 'test';
Comment #4
generalconsensus CreditAttribution: generalconsensus commentedThe answer is in altering $elements within the hook below. Address field does not allow you alter values or defaults directly in the form or form_state but rather in the addressfield_standard_form_alter :)
Comment #5
John Pitcairn CreditAttribution: John Pitcairn commentedI'd like to see an actual working example of how to use hook_form_commerce_checkout_form_checkout_alter() to set default values for a custom field (not addressfield) within the customer_profile_billing pane. The $form_state structure for this is utterly bewildering to me.
I have a process that begins with a custom form. This adds items to the user's cart in its submit function, then redirects to checkout. I've added a button to the checkout pane which takes the user back to my form and repopulates it for editing, now I'd like to repopulate the checkout form with any values that the user entered prior to going back.
Comment #6
rszrama CreditAttribution: rszrama commentedI'm afraid such support is out of the scope of this issue queue. The real problem you're going to have is in trying to recover these form values that were not saved anywhere; I'm not sure your problem as described is feasible. Your best bet will be real-time chat in a Drupal IRC channel like #drupal-support or #drupal-commerce. The only leads I can give you are $form_state['values'] will have validated form input in it when a form is submitted, so you can probably cache the data you want in the session during the redirect and then extract those values and set them as the #default_value of the related form elements in $form on return to the checkout form.
Comment #7
rszrama CreditAttribution: rszrama commentedI'm afraid such support is out of the scope of this issue queue. The real problem you're going to have is in trying to recover these form values that were not saved anywhere; I'm not sure your problem as described is feasible. Your best bet will be real-time chat in a Drupal IRC channel like #drupal-support or #drupal-commerce. The only leads I can give you are $form_state['values'] will have validated form input in it when a form is submitted, so you can probably cache the data you want in the session during the redirect and then extract those values and set them as the #default_value of the related form elements in $form on return to the checkout form.
Comment #8
John Pitcairn CreditAttribution: John Pitcairn commentedUnderstood, thanks. I'm already caching relevant $form_state data in the session and retrieving that, in both directions. It's finding and setting the appropriate checkout pane $form or $form_state values that is proving more than a little frustrating. I'll post back here if I get it working, since this issue is one of the few results for a relevant google search on the subject.
Comment #9
rszrama CreditAttribution: rszrama commentedSorry for the double post earlier. Not sure what happened. : P
If you have the Devel module on your site, you can do a
dpm($form);
to get a handy collapsed browser of the $form array. Then you can step through to the precise form elements you're going to want to alter. That's how I tend to find the right things to alter.Comment #10
generalconsensus CreditAttribution: generalconsensus commented@John Pitcairn
Just as reference here is my code as how I was able to autopopulate the shipping/billing fields. You have to figure I'm dumping my custom values into the order array for portability.
Comment #11
John Pitcairn CreditAttribution: John Pitcairn commentedAh, there's my problem. I was attempting to replace relevant parts of the $form_state variable with the cached parts, set $form_state['rebuild'] and have the form automagically repopulate itself just like standard form. Which won't fly, because I'm in hook_form_alter(), not the form builder. Well, it was 2am ;-)
So to pursue that approach and avoid manually repopulating lots of fields in $form, I guess I could try:
1 - implement hook_commerce_checkout_router() and route to a custom menu item
2 - the page callback for that calls drupal_get_form() for a custom form builder
3 - which itself calls commerce_checkout_form($form, &$form_state, $order, $checkout_page), supplying the cached $form_state.
Feasible?
Comment #11.0
John Pitcairn CreditAttribution: John Pitcairn commentedchanged the php code to be consistent with original code
Comment #12
mrweiner CreditAttribution: mrweiner commentedAnybody else landing here looking to alter the addressfield default values, you have the option of hook_addressfield_default_values_alter(&$default_values, $context) -- http://cgit.drupalcode.org/addressfield/diff/?id=bb4b1d9&id2=00476fb755e...