0

I want to prepopulate commerce billing address in checkout process and make all fields in the address field readonly.

I've tried the next code:

$form['customer_profile_billing']['#disabled'] = TRUE;

But disabling didn't help me because values are not submited when field is disabled. And I get the notice:

Notice: Undefined index: customer_profile_billing in commerce_customer_profile_copy_validate() (line 1372 of ...

I also've tried the next code but it doesn't solve anything:

  $form['customer_profile_billing']['#attributes']['readonly'] = 'readonly';
  $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#attributes']['readonly'] = 'readonly';
  $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#attributes']['#address']['readonly'] = 'readonly';

Using of '#type' => 'value' or '#markup' didn't help me too.

Here is result of dpr($form['customer_profile_billing']['commerce_customer_address']);:

Array
(
    [#type] => container
    [#attributes] => Array
        (
            [class] => Array
                (
                    [0] => field-type-addressfield
                    [1] => field-name-commerce-customer-address
                    [2] => field-widget-addressfield-standard
                )

        )

    [#weight] => -10
    [#tree] => 1
    [#language] => und
    [und] => Array
        (
            [0] => Array
                (
                    [#entity_type] => commerce_customer_profile
                    [#entity] => stdClass Object
                        (
                            [revision_id] => 196674
                            [revision_uid] => 1
                            [status] => 1
                            [log] => 
                            [revision_timestamp] => 1380789191
                            [data] => 
                            [profile_id] => 196674
                            [type] => billing
                            [uid] => 193074
                            [created] => 1372337831
                            [changed] => 1380789191
                            [commerce_customer_address] => Array
                                (
                                    [und] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [country] => DE
                                                    [administrative_area] => 
                                                    [sub_administrative_area] => 
                                                    [locality] => Ahaus
                                                    [dependent_locality] => 
                                                    [postal_code] => 48683
                                                    [thoroughfare] => Am Aabach 6
                                                    [premise] => 
                                                    [sub_premise] => 
                                                    [organisation_name] => 
                                                    [name_line] => Aabachschule
                                                    [first_name] => 
                                                    [last_name] => 
                                                    [data] => 
                                                )

                                        )

                                )

                            [rdf_mapping] => Array
                                (
                                )

                            [entity_context] => Array
                                (
                                    [entity_type] => commerce_order
                                    [entity_id] => 46
                                )

                        )

                    [#bundle] => billing
                    [#field_name] => commerce_customer_address
                    [#language] => und
                    [#field_parents] => Array
                        (
                            [0] => customer_profile_billing
                        )

                    [#columns] => Array
                        (
                            [0] => country
                            [1] => administrative_area
                            [2] => sub_administrative_area
                            [3] => locality
                            [4] => dependent_locality
                            [5] => postal_code
                            [6] => thoroughfare
                            [7] => premise
                            [8] => sub_premise
                            [9] => organisation_name
                            [10] => name_line
                            [11] => first_name
                            [12] => last_name
                            [13] => data
                        )

                    [#title] => Address
                    [#description] => 
                    [#required] => 1
                    [#delta] => 0
                    [#weight] => 0
                    [element_key] => Array
                        (
                            [#type] => value
                            [#value] => commerce_customer_profile|billing|commerce_customer_address|und|0
                        )

                    [#type] => container
                    [#handlers] => Array
                        (
                            [0] => address
                            [1] => name-oneline
                        )

                    [street_block] => Array
                        (
                            [#type] => addressfield_container
                            [#attributes] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => street-block
                                        )

                                )

                            [#weight] => 0
                            [thoroughfare] => Array
                                (
                                    [#title] => Address 1
                                    [#tag] => div
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => thoroughfare
                                                )

                                        )

                                    [#size] => 30
                                    [#required] => 1
                                )

                            [premise] => Array
                                (
                                    [#title] => Address 2
                                    [#tag] => div
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => premise
                                                )

                                        )

                                    [#size] => 30
                                )

                        )

                    [locality_block] => Array
                        (
                            [#type] => addressfield_container
                            [#attributes] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => addressfield-container-inline
                                            [1] => locality-block
                                            [2] => country-DE
                                        )

                                )

                            [#weight] => 50
                            [#attached] => Array
                                (
                                    [css] => Array
                                        (
                                            [0] => sites/all/modules/contrib/addressfield/addressfield.css
                                        )

                                )

                            [postal_code] => Array
                                (
                                    [#title] => Postal code
                                    [#size] => 10
                                    [#required] => 1
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => postal-code
                                                )

                                        )

                                )

                            [locality] => Array
                                (
                                    [#title] => City
                                    [#size] => 30
                                    [#required] => 1
                                    [#prefix] =>  
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => locality
                                                )

                                        )

                                )

                        )

                    [country] => Array
                        (
                            [#title] => Country
                            [#options] => Array
                                (
                                    [AF] => Afghanistan
                                    [AX] => Aland Islands
                                    ...<<< COUNTRIES GO HERE >>>....
                                    [ZM] => Zambia
                                    [ZW] => Zimbabwe
                                )

                            [#render_option_value] => 1
                            [#required] => 1
                            [#attributes] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => country
                                        )

                                )

                            [#weight] => -10
                            [#ajax] => Array
                                (
                                    [callback] => addressfield_standard_widget_refresh
                                    [wrapper] => addressfield-wrapper
                                )

                            [#element_validate] => Array
                                (
                                    [0] => addressfield_standard_country_validate
                                )

                            [#limit_validation_errors] => Array
                                (
                                )

                        )

                    [#wrapper_id] => addressfield-wrapper
                    [#prefix] => <div id="addressfield-wrapper">
                    [#suffix] => </div>
                    [name_block] => Array
                        (
                            [#type] => addressfield_container
                            [#attributes] => Array
                                (
                                    [class] => Array
                                        (
                                            [0] => addressfield-container-inline
                                            [1] => name-block
                                        )

                                )

                            [#weight] => -100
                            [name_line] => Array
                                (
                                    [#title] => Full name
                                    [#tag] => div
                                    [#attributes] => Array
                                        (
                                            [class] => Array
                                                (
                                                    [0] => name-block
                                                )

                                        )

                                    [#size] => 30
                                    [#required] => 1
                                )

                        )

                    [#address] => Array
                        (
                            [country] => DE
                            [administrative_area] => 
                            [sub_administrative_area] => 
                            [locality] => Ahaus
                            [dependent_locality] => 
                            [postal_code] => 48683
                            [thoroughfare] => Am Aabach 6
                            [premise] => 
                            [sub_premise] => 
                            [organisation_name] => 
                            [name_line] => Aabachschule
                            [first_name] => 
                            [last_name] => 
                            [data] => 
                        )

                    [#addressfield] => 1
                    [#process] => Array
                        (
                            [0] => addressfield_process_format_form
                        )

                )

            [#theme] => field_multiple_value_form
            [#field_name] => commerce_customer_address
            [#cardinality] => 1
            [#title] => Address
            [#required] => 1
            [#description] => 
            [#prefix] => <div id="customer-profile-billing-commerce-customer-address-add-more-wrapper">
            [#suffix] => </div>
            [#max_delta] => 0
            [#after_build] => Array
                (
                    [0] => field_form_element_after_build
                )

            [#language] => und
            [#field_parents] => Array
                (
                    [0] => customer_profile_billing
                )

        )

    [#access] => 1
)

P.S. I added ...<<< COUNTRIES GO HERE >>>.... instead of countries list

  • It seems it keeps entity inside a form, too much for me. Glad you found a way. – Mołot Oct 3 '13 at 8:56
  • Thank you for help! Yes, I've found a solution but it looks a little bit dirty. I'll post here a better one if I find it – milkovsky Oct 3 '13 at 8:58
  • @Mołot, I've updated the answer. Now validation callback it's only one string :) – milkovsky Oct 3 '13 at 10:28
1

Here is one working solution.

We can disable address field. To submit data of the addressfield we can add address value into $form_state['input'] manualy in validation callback.

In hook_form_alter disable customer_profile_shipping field.

$form['customer_profile_billing']['#disabled'] = TRUE;

Add custom validation and submit handler before others:

array_unshift($form['customer_profile_shipping']['commerce_customer_profile_copy']['#element_validate'], '_my_custom_address_validation');
array_unshift($form['#submit'], '_my_custom_address_validation');

Magic implementation of adding address values into $form_state input:

/**
 * Helper function for address field validaton and submit.
 * Implementats adding address values into $form_state input.
 */
function _my_custom_address_validation(&$form, &$form_state) {

  if (!empty($form_state['values']['customer_profile_billing'])) {
    $form_state['input']['customer_profile_billing'] = $form_state['values']['customer_profile_billing'];
  }
}

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.