5

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->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);

//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->{????????} = $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)->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();
    }
  }
1

2 Answers 2

4

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->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;
    }

    //create the right array for the save controller
    $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;
}
0

I generally find if something isn't documented, the best way to work out how to do it is by looking at someone else's code! Here's how a customer profile is added to an order in the commerce_customer module:

/**
 * Checkout pane callback: submits a customer profile edit form.
 */
function commerce_customer_profile_pane_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
  $profile = $form_state['values'][$checkout_pane['pane_id']]['customer_profile'];

  // Ensure the profile is active.
  $profile->status = TRUE;

  // Set the profile's uid if it's being created at this time.
  if (empty($profile->profile_id)) {
    $profile->uid = $order->uid;
  }

  // Notify field widgets.
  field_attach_submit('commerce_customer_profile', $profile, $form[$checkout_pane['pane_id']], $form_state);

  // Save the profile.
  commerce_customer_profile_save($profile);

  // Store the profile ID for the related field as specified on the settings form.
  $wrapper = entity_metadata_wrapper('commerce_order', $order);

  if ($field_name = variable_get('commerce_' . $checkout_pane['pane_id'] . '_field', '')) {
    $wrapper->{$field_name} = $profile;
  }
  else {
    // Or make the association in the order's data array if no field was found.
    $order->data['profiles'][$checkout_pane['pane_id']] = $profile->profile_id;
  }
}

That's assuming you have the order profile ready to go. I won't go on posting a load of code on how to do that, however you should be able to find an implementation somewhere.

3
  • thanks but I am finding this hard to get closer to a solution. Out of the code in that submit handler how is that relevant to commerce_order_save($order);
    – imp
    Feb 23, 2012 at 12:05
  • 1
    Essentially you have to add the profile ID you want to associate with the order to the $order->data['profiles'] as you can see at bottom of the code. In practice it may be a bit more complex than that - you'd have to decide which lines in the code are relevant. You should probably be adding the customer profile ID before you save the order.
    – splatio
    Feb 23, 2012 at 13:33
  • Yes your assumption was correct I just over thought the complexity here, all I needed to do was debug the $order from the controller and work out the correct format to put it all in.
    – imp
    Feb 24, 2012 at 11:49

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.