0

I am having difficulty moving a text field from the commerce billing profile entity to the account fieldset on the checkout page. No matter what I do the text field is not saved after moving it back in the custom validate function. Is there something that I'm missing?

/**
 * Implements hook_form_alter() from checkout page.
 */
function example_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {

  // Move phone number field from billing profile to account fieldset
  $form['account']['phone_number'] = $form['customer_profile_billing']['field_customer_phone_number'];
  unset($form['customer_profile_billing']['field_customer_phone_number']);

  // Declare custom validate and submit functions
  $form['buttons']['continue']['#validate'][] = 'example_checkout_form_validate'; 
  $form['buttons']['continue']['#submit'][] = 'example_checkout_form_submit';
}

/**
 * Custom checkout form validate function
 * @param type $form
 * @param type $form_state
 */
function example_checkout_form_validate($form, &$form_state) {

  // Move field back to billing profile
  $form_state['values']['customer_profile_billing']['field_customer_phone_number'] = $form_state['values']['account']['phone_number'];
  unset($form_state['values']['account']['phone_number']);
}

EDIT: I tried moving the text field back to commerce profile in a custom submit handler as shown below, but I still can't get it it work.

/**
 * Custom checkout form submit function
 * @param type $form
 * @param type $form_state
 */
function example_checkout_form_submit($form, &$form_state) {

  // Move field back to billing profile
  $form_state['values']['customer_profile_billing']['field_customer_phone_number'] = $form_state['values']['account']['phone_number'];
  unset($form_state['values']['account']['phone_number']);
}

I want to move this field, which is for the customer's phone number, on the checkout page for aesthetic purposes. In the current workflow the shipping address is shown first followed by the billing address. Since the phone number field is attached to the billing profile, it looks strange when "My Billing information is the same as my Shipping information." is check as shown below.

enter image description here

This is the last part of the checkout page redesign. I am so close to finishing it. Any help would be much appreciated.

Thanks Paul

EDIT

I was finally able to figure this out with the help of Ryan Szrama and http://www.drupalcommerce.org/questions/3711/saving-customer-profile-field. Thank you guys.

function example_checkout_form_submit($form, &$form_state) {
  $order = $form_state['order'];
  $phone = $form_state['values']['account']['phone_number'][LANGUAGE_NONE][0]['value'];
  $po = $form_state['values']['account']['po_number'][LANGUAGE_NONE][0]['value'];

  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // get profile id
  $profile_id = $order_wrapper->commerce_customer_billing->profile_id->value();

  // load customer profile
  $profile = commerce_customer_profile_load($profile_id);
  if ($profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile)) {
    // set the value of the field and save the profile
    $profile_wrapper->field_customer_phone_number->set($phone);
    $profile_wrapper->field_customer_po_number->set($po);
    $profile_wrapper->save();
  }
  unset($form['account']['phone_number']);
  unset($form['account']['po_number']);
}
1

Unfortunately, you can't move this and have it continue working as normal. Entity form submission depends on a specific "tree" hierarchy in the submitted field values, and moving it like this disrupts that. Thus when the customer profile information is saving, your custom textfield is just invisible.

If you really need to move this, then you should add a custom submit handler that populates the customer profile as before.

  • Thank you for you response Ryan. Unfortunately, I tried what you suggested as shown above and I still can't get it to save. I feel like I am close though. Do you see any errors I might have overseen? Thanks – PaulJ Jan 12 '14 at 16:50

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.