retrieve order_id onBeforeOrderCreate

  • Posts: 20
  • Thank you received: 0
13 years 6 months ago #16789

is there a way to retrieve order_id onBeforeOrderCreate?

otherwise, what would be the best way around it?

I am looking to link an id (not hikashop table) created before order to the newly created order_id (hikashop_order).

I am quite desperate now. Please help. :(

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
13 years 6 months ago #16790

The event onBeforeOrderCreate is called, as the name tells you, before the order is created. So you don't have the order_id at that time since the order_id is generated automatically by MySQL when a new entry in the order table is inserted.
If you really need it at that point, the only way I can see (which is not perfect), would be to do a query and load the max order_id of the order table and increment it so that you get the next order_id which will be used and there is a very high probability that there won't be any order created between that time and the creation of the order.
But maybe you should do it differently: add a custom order field and use the unique_id() PHP function to generate an unique id that you can put in the custom field, use it in your code, and then you will be able to identify the order with that unique id.

Please Log in or Create an account to join the conversation.

  • Posts: 20
  • Thank you received: 0
13 years 6 months ago #16791

hmm I found another way around it (I just realised cart is only deleted after onAfterOrderCreate is triggered) so before the order is created, i retrieve the cart id and put into another table, then after the order is created, i retrieve the newly created order_id by matching the cart_id's.

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
13 years 6 months ago #16792

That's also a possibility indeed.

Please Log in or Create an account to join the conversation.

  • Posts: 20
  • Thank you received: 0
13 years 6 months ago #16795

on a related question, can you tell me what variables can I expect in $order->cart within onAfterOrderCreate function? would cart_product_id be in there?

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
13 years 6 months ago #16799

You have all the information you want including the cart_product_id but cart_product_id is for each products of the cart. So it's in a products array and not directly in that object. For example, for the first product of the cart:
$order->cart->products[0]->cart_product_id;

Please Log in or Create an account to join the conversation.

  • Posts: 20
  • Thank you received: 0
13 years 6 months ago #16801

:( no luck, i tried the following in onAfterOrderCreate and cart_product_id is empty:

echo "cartproductid is ".$order->cart->products[0]->cart_product_id;

Last edit: 13 years 6 months ago by zerocul. Reason: typo

Please Log in or Create an account to join the conversation.

  • Posts: 82867
  • Thank you received: 13374
  • MODERATOR
13 years 6 months ago #16810

As a general advice, you should include the library dbug on your website: dbug.ospinto.com/
and then use that code:

new dbug($order);
exit;

That will display you nicely the whole object and you will be able to easily see where the information you're looking for is stored.

I just checked it on our end and the cart_product_id of the products is not given to the event onAfterOrderCreate.

Please Log in or Create an account to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum