In our case we're using the following code(pattern) to create a (Drupal Commerce) address profile

 $profile = commerce_customer_profile_new('billing', $newUserId); 
 $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
 $profile_wrapper->commerce_customer_address = array_merge(addressfield_default_values(), array(
     'country' => 'DE', 
     'last_name' => $newlname,
     'postal_code' => $plz,
     'thoroughfare' => $addrline.' '.$houseno,
     'locality' => $city,
   ));
 $profile_wrapper->save();

This works fine.

We have now added a custom field title (a term reference) to the billing profile (at: /admin/commerce/customer-profiles/types/billing/fields). This is the standard manage fields dialog (I suppose) in this case for the "profile type".
Again this works fine, title appears as a select dropdown in the GUI when making an order.

My question is how can we get and more importantly SET this value programatically (e.g. in the code snippet above).
Naively I tried something like this:

$profile_wrapper->field_title[LANGUAGE_NONE][0]['tid'] = $title;

which doesn't work. (crashes) The Apache errlog shows:
PHP Fatal error: Cannot use object of type EntityValueWrapper as array

All I've found is https://drupal.stackexchange.com/questions/162396/cant-access-custom-fie... but to be honest I don't know where to place hook_field_info (what the hook part would be in my case).

Thanks

Comments

jamescook created an issue. See original summary.

jamescook’s picture

Or something like(?):

  $profile_wrapper->field_title = array_merge(array(), array(
    'title' => $title,
  ));

I CAN see field_title at the same level as [commerce_customer_address] in the wrapper:

[propertyInfo:protected] => Array
        (
            [properties]
                 [commerce_customer_address]
                   :
                 [field_title] => Array
                        (
                            [label] => Title
                            [type] => taxonomy_term
                            [description] => Field "field_title".
                            [getter callback] => entity_metadata_field_property_get
                            [setter callback] => entity_metadata_field_property_set
                            [access callback] => entity_metadata_field_access_callback
                            [query callback] => entity_metadata_field_query
                            [translatable] => 1
                            [field] => 1
                            [required] => 1
                            [bundle] => title
                            [options list] => entity_metadata_field_options_list
                        )

This also crashes, the syslog message is:

Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set()

jamescook’s picture

Issue summary: View changes