Discussions

Programatically Create Order with Customer Profile

I am using this method below 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.

<?php
 

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->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->commerce_line_items[] = $line_item;
    }

 
// Save the order.
 
commerce_order_save($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)->commerce_line_items as $delta => $line_item_wrapper) {
    if (
$line_item_wrapper->order_id->value() == 0) {
     
$line_item_wrapper->order_id = $order->order_id;
     
$line_item_wrapper->save();
    }
  }

  return
$order;
}
?>
Posted: Feb 22, 2012

Comments

imp7 on February 24, 2012

This works correctly with the controller save.

<?php
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->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->commerce_line_items[ ] = $line_item;
    }


   
$profile_object = array (
       
'und' => array ( array ( 'profile_id' => $profile_id , ) , ) , );
   
   
   
$order->commerce_customer_billing = $profile_object;

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

    return
$order;
}
?>

Joel Wallis Jucá on October 26, 2012

I had this problem since yesterday and after many hours of searching and reading documentation I found a solution. There is an example of doing this.

<?php
/**
 * @file
 *
 * Let's assume that we are writing in a feature or module, mymodule.module.
 */

/**
 * hook_commerce_checkout_router() used to create the customer profile
 * programmatically. Read the hook documentation: <a href="http://bit.ly/VO0ERY
">http://bit.ly/VO0ERY
</a> */
function mymodule_commerce_checkout_router($order, $checkout_page) {
 
// 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();
  ));

 
// Now you have a customer profile with a prepopulated addressfield, so,
  // modify it to your needs. To edit the name, do:
 
$profile[LANGUAGE_NONE][0]['name_line'] = 'My Lorem Ipsum Full Name';

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

 
// Now just associate the customer profile to your order. Remember that the
  // $order variable is being passed as argument to this hook.
 
$order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $profile->profile_id;

 
// Now you can save the order.
 
$commerce_order_save($order);
}
?>

I've not tested this code. But you can use it as starting point to get your solution. Also, I advise you to read the documentation of the follow functions:

hook_commerce_checkout_router(): http://bit.ly/VO0ERY

commerce_customer_profile_new($profile): http://bit.ly/R7tMfk
commerce_customer_profile_load($profile_id): http://bit.ly/R7uHMP

commerce_order_load($order_id): http://bit.ly/Y4sKFY
commerce_order_save($order): http://bit.ly/WOXht9