Creating custom order: can't reach checkout
Hi,
I'm developing a site with kickstart 2.0 which entails a custom form to generate an order with some additional metadata attached (the purpose is for activity bookings, though the site will be selling goods too). I've added the extra fields to my orders in Admin > Store Settings > Manage fields, designed the form and given it a submit handler that calls my function that creates the order. I'm basing the function on Ryan's blog article on the subject, here it is:
<?php
function booking_create_order(&$form_state) {
global $user;
$prod_ids = array(
array(
'A' => 4,
'K' => 5
),
array(
'A' => 6,
'K' => 7
)
);
$order = commerce_order_new($user->uid, 'checkout_checkout');
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
//Set values for my added fields
$order_wrapper->field_is_booking = true;
$order_wrapper->field_contact_phone = $form_state['values']['phone'];
$tour = explode('-', $form_state['values']['time']);
$order_wrapper->field_booking_date_time = strtotime(
$form_state['values']['date'].' '.$tour[0].':00');
//Get Product IDs and qtys based on form data
$prods = array($prod_ids[$tour[1]]['A'] => $form_state['values']['adult']);
if($form_state['values']['child'] > 0) {
$prods[$prod_ids[$tour[1]]['K']] = $form_state['values']['child'];
}
commerce_order_save($order);
foreach($prods as $id => $qty) {
$product = commerce_product_load($id);
$line_item = commerce_product_line_item_new($product, $qty, $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 fago's rockin' wrapper.
//$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
commerce_order_save($order);
}
//Redirect
$form_state['redirect'] = 'checkout/'.$order->order_id;
//drupal_goto('checkout/'.$order->order_id);
}
?>
This does succeed in creating the order with the desired extra fields, but the redirect fails and I end up on the detail page for one of my other (goods) products, which (although I'm not sure why) seems to be the same page I land on for any made-up site URI (eg "http://mysite.com/notarealpage").
I've also tried redirecting to other parts of the checkout process (eg /checkout/{order_id}/checkout or /checkout/{order_id}/shipping) but the same thing happens.
So is the order not "ready" for checkout? Do I also need to instantiate a cart? The article mentioned that the checkout is not dependent on the cart, but perhaps this isn't quite true?
Any advice would be very welcome!
Comments
Self-solved :)
OK I took the path of least resistance and figured out how to make it a "cart" order, by changing the order creation line as follows:
<?php
//Old: $order = commerce_order_new($user->uid, 'checkout_checkout');
$order = commerce_cart_order_new($user->uid);
?>
I think I misunderstood Ryan's comment in the article: Orders might not be dependent on the Cart, but the Checkout apparently is (I suppose checkout is considered a sub-module of Cart, in effect). Certainly I didn't get close to finding a way to get an order through checkout without a cart, though if this is supported I'd be interested to hear about it...!