0

im trying to save into a customer profile field. Reading it out is no problem:

if(!empty($order_wrapper->commerce_customer_billing->field_shop_annotations)) {
$annotations = $order_wrapper->commerce_customer_billing->field_shop_annotations->value();
}

But saving into gives me an error message. Here's my code:

// Save annotations
$order = commerce_cart_order_load($user->uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$annotations = $form_state['values']['annotations'];

$profile_id = $order_wrapper->commerce_customer_billing->profile_id->value();

$profile = commerce_customer_profile_load($profile_id);

if ($profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile)) {
   // This gives me the error message:
   $profile_wrapper->commerce_customer_billing->field_shop_annotations->set('muh');
}

Everthing works fine except the set method inside the if clause. I also tried:

$profile_wrapper->commerce_customer_billing->field_shop_annotations->value = $annotations;
2

ok i got it:

$annotations = $form_state['values']['annotations'];
global $user;

// load oder
$order = commerce_cart_order_load($user->uid);
$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_shop_annotations->set($annotations);
       $profile_wrapper->save();

}

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.