1

I am developing api access point for drupal commerce, from that access point user can upload his/her billing information during checkout process. Here is my code, but it can not add user full name in billing information. Any suggestion is greatly appreciated.

Thanks

  function commerce_services_bill_ship_create($data, $flatten_fields) {
    $status; $message;
    $orderId; $uid;

    $orderId =$data["orderId"];
    $uid = $data["uid"];
    $fullname = $data['fullname'];

    $order = commerce_cart_order_load($uid);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $order_wrapper->commerce_customer_billing->commerce_customer_address->name_line = $fullname;
    $order_wrapper->commerce_customer_billing->commerce_customer_address->thoroughfare = "No.1, ABC street";    

    $order_wrapper->commerce_customer_billing->commerce_customer_address->locality = "New York";
     try{
        // Save the order.
        commerce_order_save ($order);
        $status = 1;
        $message = "order update successfully.";
    }catch(Exception $ex){
        $status = 0;
        $message = "order can't update successfully.";
    }
    $reply= (array('status' => $status,'message' => $message));

    return $reply;
 }

enter image description here

2 Answers 2

1

To add billing information in checkout process first we have to create a profile object and assign it to the order. This code works for me

For adding new profile for billing information

function commerce_services_bill_ship_create($data, $flatten_fields) {
$status; $message;
$orderId; $uid;

$orderId =$data["orderId"];
$fullname = $data['fullname'];
$city = $data["city"];
$address = $data["address"];

//load order that you want to add billing information
$order = commerce_cart_order_load($uid);


// We'll assume that the current user will be the customer of our order
global $user;

$profile = commerce_customer_profile_new('billing', $user->uid);
$profile->commerce_customer_address = array(LANGUAGE_NONE => array(0 => addressfield_default_values()));

//for updating existing profile, uncomment the line below and comment commerce_customer_profile_new()
//$profile = commerce_customer_profile_load($profile_id); //

//add fullname
$profile -> commerce_customer_address['und'][0]['name_line'] = $fullname;

//add address
$profile->commerce_customer_address['und'][0]['thoroughfare'] = $address;


//add city
$profile->commerce_customer_address['und'][0]['locality'] = $city;


// You sould pass the entire $profile object as parameter.
commerce_customer_profile_save($profile);

//assign profile_id to the order
$order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $profile->profile_id;

try{
    // Now you can save the order.
    commerce_order_save($order);
    $status = 1;
    $message = "order update successfully.";
}catch(Exception $ex){
    $status = 0;
    $message = "order can't update successfully.";
}


$reply= (array('status' => $status,'message' => $message));

return $reply;
}

Reference site - Programatically Create Order with Customer Profile , Creating Customer Profiles Programmatically

1
  • You can still use wrappers here, bit this is correct because it replaces the reference value. With a wrapper you just need $wrapper->commerce_customer_billing = $profile then save order. Commented Feb 10, 2016 at 12:16
0

You are saving the order, which would have the state that it held before you made it into a wrapper. You need to save the $order_wrapper:

$order_wrapper->save();
2
  • I have tried as u said, but no billing information updated or changed. It seem to me I am following the wrong way, I think I have to create user profile and have to assign in order. drupalcommerce.org/discussions/2615/…
    – SAWAUNG
    Commented Feb 10, 2016 at 5:25
  • 1
    You don't need ->save(), objects are always passed by reference, so $order was saved Commented Feb 10, 2016 at 12:14

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.