I want to fetch the latest Ubercart order's associated uid and if it is NOT the same as the newest user's ID then my Rule should validate and send an email to a selected address.

In the Rule I created, I've written the following PHP action in order to first check what $uids[0] is giving me. When this works as require, I'll put the same code as a PHP condition, replacing the watchdogs with return FALSE & TRUE.

The problem I'm facing is that $uids[0] always gives 0 instead of the last order's user ID.

$query = db_query("SELECT uid, created FROM {uc_orders} order by created desc");
$uids = array();
while ($records = db_fetch_array($query)) {
    $uids[] = $records['uid'];
$latestaccountuid = $account->uid;
$latestorderuid = $uids[0];

$message = "Latest account id:".$latestaccountuid." .Latest order uid:".$latestorderuid;

if ($latestaccountuid == $latestorderuid) {
watchdog('FALSE', $message);
//return FALSE;
else {
watchdog('TRUE', $message);
//return TRUE;
  • 1
    Orders for anon users will probably go in with a 0 uid, try filtering those out (WHERE uid <> 0). BTW if you just want a single result you can use db_query_range() and db_result() to make your code a bit more efficient
    – Clive
    Jan 21 '14 at 17:47
  • The user is initially anonymous, that's true. But after checking out, each order is associated to a newly registered user (registration happens automatically on order completion). The db_query would not work if order's weren't connected to uids. The weird part is that whenever I put the same query in a block and echo $uids[0] the correct uid is printed...
    – webmaniac
    Jan 21 '14 at 19:46

I think the problem is with your $account variable. I don't see where it's instanciated in your code. It's probably related to the current logged in user, so you've to find a way to access this information from Rules. Try to use dsm($account); if you're using the devel module and check if the variable is empty.

  • The $account variable is supposed to be available for whatever code I write in the Rule's PHP condition. It's mentioned clearly above the PHP box. This variable represents the user related to the Rule's action (in this case, the action is whenever a new user account is created). I don't see a reason why this variable is empty as it's also suggested by the Rule.
    – webmaniac
    Jan 21 '14 at 19:41
  • Just figured it our - I should be using the $user variable instead of $account. $user is the new user registering...!
    – webmaniac
    Jan 24 '14 at 9:57

$account is the account of the logged in user. when you are testing it, are there any orders assigned to the account you are logged in with?

I the Rules UI:

$account User logged in user


dsm() is legacy function. It's a call to dpm() as it is, so you should use dpm()

  • When I test it, I test it with an anonymous user. I use a personal email address which is not stored in the db as I delete this user and his orders before testing again. dpm() is what I use anyway.
    – webmaniac
    Jan 24 '14 at 9:05

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.