2

I'm using Drupal Commerce 7.x. I have added some fields to the checkout form in the customer profile. Specifically I've added 'Title' and 'Organization'. I'd like to move some of the fields around because they aren't in the order I'd like, but I can't figure out how to move them.

For example, right now the order is: 1. Country (I def. don't want that first) 2. Full name 3. Address etc.

I want it like this:

  1. Full Name
  2. Title (new field)
  3. Organization (new field)

I tried using a module that does this (just as an example, obviously, since only two fields are listed here). Am I on the right track?

<?php
function CommerceAlter_form_commerce_checkout_form_alter(&$form, 
&$form_state, &$form_id) {
   $order = array('edit-customer-profile-billing-field-title', 
'addressfield-container-inline');
    foreach ($order as $key => $value){
      if (isset($form[$value])){
        $form[$value]['#weight'] = $key;
      }
    }
}

But it didn't do anything.

Note: I was unsure how to reference the fields. The first one above called 'field-title' is the ID for that field and the second listed for addressfield (which is in the form by default) is by class .addressfield-container-inline).

3 Answers 3

2

Changing field order on the checkout form can be done by going to admin/commerce/config/checkout

Then just drag the items up and down to arrange them in the order you wish.

3
  • 1
    except that some of the fields I want to re-order are included in the 'Address' field. If you go to admin > store > customer profiles > profile types > billing information you'll see what I mean. It just says 'Address'. But that includes a bunch of fields and I don't know how they are part of the Address field but it includes name, city, state, etc. and I want to re-order those with my new fields I've added.
    – Sage
    Sep 11, 2015 at 4:15
  • 2
    The Address field is from the Address field module which drupal commerce includes. You control everything through views. In views you can remove the default address grouping and add in individual address fields in any order you like. The view is Customer Profiles. Sep 11, 2015 at 7:05
  • @CraigBowman nice explanation, thanks - but 4 years later i dont have a "Customer Profiles" view. and i have the same problem, we need to split up the address form parts for example.
    – netzding
    Nov 14, 2019 at 12:31
0

Per Commerce module maintainer, there's no good way to move the pieces of the Address composite field. https://www.drupal.org/project/commerce/issues/3016940. The maintainer suggested CSS, but any solution that changes the display but not the code (e.g., flexbox) can be problematic from a semantic and accessibility standpoint. For example, reordering elements visually can create confusion when using screenreaders https://www.w3.org/TR/css-flexbox-1/#order-accessibility.

It's also worth noting that country is intentionally the first field. Some countries use Last name and then First name. So, changing the country to Japan, for example, changes the order of the name fields.

0

You have to make sure they are in the order you want in the render array. When setting #weight does not work, you can change the order by removing and re-adding items from the render array.

$shipping_profile = $variables['elements']['shipping_profile'];
unset($variables['elements']['shipping_profile']);
$variables['elements']['shipping_profile'] = $shipping_profile;

This removes the element 'shipping_profile' from its current place in the render array and places it back at the end of the render array, causing it to be shown last on the checkout pane you are editing. I myself used this on a hook_preprocess_commerce_checkout_pane__shipping_information hook to alter the order of some items inside the shipping information in my checkout flow.

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.