Tags/topics: 
1
Answers
Vote up!
1
Vote down!

Saving into a customer profile field

Hey,

Hey, 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;
Asked by: maze
on August 4, 2012

1 Answer

Vote up!
2
Vote down!

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();
                 
    }
Answer by: maze
Posted: Aug 17, 2012

Comments

You could also try just:

<?php
    $order_id
= 12;
   
$order = commerce_order_load($order_id);
   
$wrapper = entity_metadata_wrapper('commerce_order', $order);
   
$wrapper->commerce_customer_billing->value()->field_shop_annotations->set('my_value');
   
$wrapper->save();
?>
- davidwhthomas on April 30, 2013