1

I'm just trying to create an order in code like this:

$selected_month = $form_state['values']['month'];
$selected_year = $form_state['values']['year'];
$order = commerce_order_new($form_state['values']['user'], 'checkout_checkout');
commerce_order_save($order);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$product = commerce_product_load($form_state['values']['product']);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
commerce_line_item_save($line_item);
$order_wrapper->commerce_line_items[] = $line_item;
dpm($order_wrapper->value());
$order_wrapper->save();

This in inside an form submit callback, so I have all the data needed like productId, userId, etc., but suddenly it stopped working. Apache says nothing and can't see MySQL logs but i contacted my provider and says that there is no logs either.

I'm using commerce_addressbook, but not using commerce_coupon or commerce_discount. Not altering the order_id in rules.

Looks like some kind of order_id problems but not sure, it looks like the second order_save (with wrapper function or commerce_order_save) tries to save the order but with the same order_id.

In the database the order is created with its line item

I've tried to clear all the caches, same result.

A thing which i find strange to me, is its revision id, in the database even tought order_id is 160, for example, its revision id is 552.

Another strange behaviour i found is that the commerce_order_total is not set at all, the row in the table is created but with 0 amount.

Same result for both saving functions, tried both but it is always in the second call of the order save, I comment that line and it works.

Commenting that line works, but of course no line items, but the rows are created for both entities, commerce_order and commerce_line_item. I mean, i see them in the data base but in the edit page of that created order, there is no line items

Here is the content of $order_wrapper->value():

dpm: checkout_checkout, commerce_order, commerce_line_items, line_item_id

  • Do you have any other warning/error apart of TransactionOutOfOrderException? – kenorb Oct 24 '17 at 18:27
  • Just that :( Apache says nothing and can't see mysql logs but i contacted my provider and says that there is no logs either – Gianni Di Falco Oct 24 '17 at 18:30
  • On which line it fails? During commerce_line_item_save() or $order_wrapper->save()? What's the value of $order_wrapper->value() which you're printing? – kenorb Oct 24 '17 at 18:33
  • Another strange behaivor i found is that the commerce_order_total is not set at all, the row in the table is created but with 0 amount – Gianni Di Falco Oct 24 '17 at 18:33
  • Same result for both, tried both but it is always in the second call of the order save, i comment that line and it works – Gianni Di Falco Oct 24 '17 at 18:34
2

Basically the TransactionOutOfOrderException exception is triggered by transaction being rolled-back (see: rollback()) called by Commerce module which was triggered by inability to save your entity (such as validation errors like saving empty values for required fields). Check commerce_line_item.controller.inc or commerce_order.controller.inc files for more details when exactly rollback is triggered.

Since you're using entity metadata wrappers, it is strongly recommended that you should implement try...catch section to catch exceptions and add debugging errors into watchdog. Here is the sample syntax of saving node wrapper:

try {
  $node_wrapper = entity_metadata_wrapper('node', $node);
  $price = $node_wrapper->field_product->field_price->value();
} 
catch (EntityMetadataWrapperException $exc) {
  watchdog(
    'MODULE_NAME',
    'EntityMetadataWrapper exception in %function() <pre>@trace</pre>',
    array('%function' => __FUNCTION__, '@trace' => $exc->getTraceAsString()),
    WATCHDOG_ERROR
  );
}

So adequately your code should look like:

if (empty($form_state['values']['product'])) {
  drupal_set_message(t('Missing product on the submit form! Form values: @values',
    array('@values' => print_r($form_state['values'], TRUE)), 'error');
  return;
}
// @todo: Do above condition for other parameters as well.
$selected_month = $form_state['values']['month'];
$selected_year = $form_state['values']['year'];
$order = commerce_order_new($form_state['values']['user'], 'checkout_checkout');
commerce_order_save($order);

try {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $product = commerce_product_load($form_state['values']['product']);
  $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
  commerce_line_item_save($line_item);
}
catch (Exception $e) {
  drupal_set_message(t('Error saving line item: @item, message: @error.',
    array('@item' => print_r($line_item, TRUE), '@error' => $e->getMessage())), 'error');
  watchdog_exception('mymodule', $e);
}

try {
  $order_wrapper->commerce_line_items[] = $line_item;
  $order_wrapper->save();
catch (Exception $e) {
  drupal_set_message(t('Error saving order: @order, message: @error.',
    array('@order' => print_r($order_wrapper, TRUE), '@error' => $e->getMessage())), 'error');
  watchdog_exception('mymodule', $e);
}

Note: Test the above code first on your testing environment.

So basically by implementing more logging into your code, you can see further what's missing and why it's failing.

For further debugging, ideally you should setup your local environment with copy of the database and reproduce the problem on your development machine.

Since you're dealing with submit handler, for easier debugging, you can use DevTools (in the supported browser) where you can repeat XHR requests, so you can see results after code changes without filling out the complex forms. If your form will require different token on each submit, ideally you should add die()/exit(); to prevent the form being completely submitted, this way you can test your form using quick and repeatable method.

For more sophisticated debugging, you should use step-by-step debugger by using some IDE editors which can support that, by enabling XDebug PHP extension. Or generate the trace file which can help you to find out more about your issue.

  • 1
    Jesus christ... I had a required field in the ´commerce_line_item´ entity so i set it up before the second order save. I guess i was blinded by the fact that it was working before (and that cron was not settled up and cache maybe...) and that the error message is not very descriptive. Nonetheless, thank you very much for your help!!! – Gianni Di Falco Oct 24 '17 at 19:34

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.