1

Is it possible to determine who created the order? I've installed the entity reference module, then added a entity reference field in the order, but I'm not sure if this will work.

I've used the hook_form_alter() to get the current user id and set the current username as a default value.

How to check and save the current user who created the order upon saving the order? Will only set the default value on first save, then it won't change anymore in the next save.

Below is my code:

$user = $GLOBALS['user'];
if ($user->uid) {
  $form['field_order_created_by'][LANGUAGE_NONE][0]['target_id']['#default_value'] = $user->name;
}
  • Please explain in more detail about your questions. Which order are you referring to? Which kind of order this is? What is first save? – ErangaJ Apr 26 '15 at 4:20
  • I mean, after saving a new commerce order. There are several admins in the website that I need to know whose created the order. Or can it be done in rules? – Madelyn Cruz Apr 26 '15 at 5:07
1

What you can do is you can create a small table which keeps your order id, user id and anything else you want. Whenever the order is saved you can check whether a record exists for the particular order id. If there is no you add the record otherwise you don't change the current record. For that you can use hook_commerce_order_presave(). Inside this hook you can use your code l

option 1

function yourmodule_commerce_order_presave($order){
    if(!db_result(db_query('SELECT * FROM {your_table} WHERE orderid = %d', $order->id)))
        $user = $GLOBALS['user'];
        if ($user->uid) {
            $form['field_order_created_by'][LANGUAGE_NONE][0]['target_id']['#default_value'] =       $user->name;

        //add new record to yuor table
    }

}

option 2

function yourmodule_commerce_order_presave($order){
    if(!check_record_exists($order->id))
        $user = $GLOBALS['user'];
        if ($user->uid) {
        $form['field_order_created_by'][LANGUAGE_NONE][0]['target_id']['#default_value'] =       $user->name;
    //add new record to your table
}

function check_record_exists($oid){
    $exists = db_result(db_query('SELECT * FROM {your_table} WHERE orderid = %d', $oid));
}
  • Hi Erangaj, I have no idea if from where is the "orderid" comes from. I don't have enough knowledge in mysql. – Madelyn Cruz May 6 '15 at 3:47
  • "orderid" is related to commerce database table. You can find it in the database. – ErangaJ Oct 22 '15 at 5:58

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.