Commerce programatically Create Order with Customer Profile I am using this method to create order successfully, how do I add a customer profile with all the country/address etc to this order?

There does not seem to be any reference to customer profiles in commerce_order_save. I had a peek into the commerce_order database table and it also must be hidden in data.

function create_order_with_products($product_ids, $uid) {
    $order = commerce_order_new($uid, 'pending');

    // Save the order so its ID is assigned.
    commerce_order_save($order);

    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

    //add products to order by ids array
    foreach ( $product_ids as $product_id ){
        $product = commerce_product_load($product_id);

        // Create a line item with quantity 1 and this product.
        $line_item = commerce_product_line_item_new($product, 1, $order-&gt;order_id);

        // Save the line item to get its ID.
        commerce_line_item_save($line_item);

        // Add the line item to the order using the wrapper.
        $order_wrapper-&gt;commerce_line_items[] = $line_item;
    }

  // Save the order.
  commerce_order_save($order);

//somehow attach the profile to the order????
$profile = commerce_customer_profile_load(exampleID);

//How do I use this wrapper maybe?
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$wrapper-&gt;{????????} = $profile;

      return $order;
    }


  // Ensure the attached line items are associated with the order if they do not
  // have an order_id set yet.
  foreach (entity_metadata_wrapper('commerce_order', $order)-&gt;commerce_line_items as $delta =&gt; $line_item_wrapper) {
    if ($line_item_wrapper-&gt;order_id-&gt;value() == 0) {
      $line_item_wrapper-&gt;order_id = $order-&gt;order_id;
      $line_item_wrapper-&gt;save();
    }
  }
After running a debug on the $order object in the commerce_order.controller.inc save method it was simple to work out the right format for the data. But this is probably not best practice but is useful for generating a bunch of orders.

function create_order_with_products ( $product_ids , $uid, $profile_id ) {
    $order = commerce_order_new ( $uid , 'pending' );

    // Save the order so its ID is assigned.
    commerce_order_save ( $order );

    $order_wrapper = entity_metadata_wrapper ( 'commerce_order' , $order );

    //add products to order by ids array
    foreach ( $product_ids as $product_id ) {
        $product = commerce_product_load ( $product_id );

        // Create a line item with quantity 1 and this product.
        $line_item = commerce_product_line_item_new ( $product , 1 , $order-&gt;order_id );

        // Save the line item to get its ID.
        commerce_line_item_save ( $line_item );

        // Add the line item to the order using the wrapper.
        $order_wrapper-&gt;commerce_line_items[ ] = $line_item;
    }

    //create the right array for the save controller
    $profile_object = array ( 
        'und' =&gt; array ( array ( 'profile_id' =&gt; $profile_id , ) , ) , ); 

    $order-&gt;commerce_customer_billing = $profile_object;

    // Save the order.
    commerce_order_save ( $order );


    return $order;
}