0
function create_init_order() {
  global $user;

  $order = commerce_cart_order_load($user->uid);

  if(!$order) {

    $new_order = commerce_order_new($user->uid, 'pending');


    // just consider i have generateEmail() and randomName($len) functions

    $new_order->order_number = $order_number = randomName(10);
    $new_order->mail = generateEmail();
    $new_order->hostname = randomName(10);

    commerce_order_save($new_order);
    unset($new_order);

    $order = commerce_cart_order_load($user->uid); 
    $order->data['suburbs'] = $_REQUEST['suburb'];
    $order->data['truck'] = $_REQUEST['truck_size'];
    commerce_order_save($order);

  }
}

Hi guys this is for anonymous user creation of order if not existing yet, anything I missed why is not working well.

Specifically this part:

    $order = commerce_cart_order_load($user->uid); 
    $order->data['suburbs'] = $_REQUEST['suburb'];
    $order->data['truck'] = $_REQUEST['truck_size'];
    commerce_order_save($order);

As i need suburbs and truck data before adding the line item.

Thank you in advanced.

  • Can you elaborate on "not working well"? – David Thomas Oct 25 '13 at 3:34
  • $order->data['suburbs'] = $_REQUEST['suburb']; $order->data['truck'] = $_REQUEST['truck_size']; are not saved! – axscode Oct 25 '13 at 3:40
0

You are unsetting $new_order and then calling commerce_order_save($new_order); when $new_order is null. It should use $order there. i.e.

not:

commerce_order_save($new_order);
unset($new_order);

$order = commerce_cart_order_load($user->uid); 
$order->data['suburbs'] = $_REQUEST['suburb'];
$order->data['truck'] = $_REQUEST['truck_size'];
commerce_order_save($new_order);

but instead:

commerce_order_save($new_order);
unset($new_order);

$order = commerce_cart_order_load($user->uid); 
$order->data['suburbs'] = filter_xss($_REQUEST['suburb']);
$order->data['truck'] = filter_xss($_REQUEST['truck_size']);     
commerce_order_save($order); // here

It also looks like there's no need to save and reload the order there when one save would suffice.

Edit: OP has changed the $new_order code typo.

No need to save and load the order, the order_id is already in $new_order->order_id after saving in commerce_order_save($new_order);

Your problem is probably a NULL or empty $_REQUEST var. Alternatively, you could add the fields to the order entity itself via the Fields UI.

  • Hi David, i actually edit my post and add more info. Can you double check please, i am actually unsetting $new_order (just to make sure) and load it on $order and save $order with data. – axscode Oct 25 '13 at 4:08
  • @axscode, so you fixed the $new_order / $order variable mixup there? If so, and problem persists, answer updated with other alternative solution. – David Thomas Oct 25 '13 at 7:18
  • Yeah, problem still persist :( – axscode Oct 26 '13 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.