10 replies [Last post]
BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Was this information Helpful?

Hello UC,

I remember reading somewhere that it is possible to set a CA trigger based on elapsed time since a customer completed checkout, but I've searched and searched and can't find it.

UC 6.x-2.6 @ admin/store/ca/add

I created a trigger for "Customer completes checkout", and added the condition for "Check the order status" = "Pending". This means the order was placed and its waiting for us to begin processing it.

Now all I need to do is add a condition for time since pending, or time elapsed in the pending status, but I can't figure it out. What am I missing?

The only condition related to time that I see is for "Drupal > Check the current date", which is good as I'll need the current date, but I also need the order creation date to compare this against...

I looked at the "Execute custom PHP code" but it only lists $order and $account as variable options. Are their other variables, like maybe $order->creation_date and $order->status, or equivalent, i could use?

Would someone please help meh? Thanks!!
BigMike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Ok,I am gonna tackle this

Ok,

I am gonna tackle this using the custom PHP code option.

I dug around the code for a while... and have a question: Will this work? Are these the right variables I need to use:

if (time() > ($order->created+86400)) { // The order was created at least 24 hours ago..... }

(86400 = number of seconds in 24 hours)

If this is correct, then what else do I need to do in order to return TRUE for the custom PHP code Conditional Action? (I've never done this before)

I tried simply entering...

if (time() > ($order->created+60)) { return TRUE; }

...and configured the action to add a simple admin comment to the order.

I then completed an order, waited 60 seconds, and then checked the admin comments and nothing was added.

How exactly do we enter the PHP code to get the CA system to use it?

Thanks!
Mike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Conditional Action for time elapsed since checkout?

Been testing this all afternoon before I posted the above reply.

I was at a loss and trying to rethink the obvious and I think I see why it's not working...

The trigger I am using is "Customer completes checkout". Well, the current time that the order was created will never be greater than the time the order was created because they are one in the same.

So I am gonna look into a different trigger now.

Mike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Hmmmmmmmmmmmmm Now....

Hmmmmmmmmmmmmm

Now.... looking at the available triggers.... I don't think this is even possible with Conditional Actions..... :-/

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Conditional Action for time elapsed since checkout?

Question: Is there a way to send PHP code to the Drupal cron process? Then I can just have a cron job check the order status & how much time has elapsed and be done with it all. What do you think?

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Conditional Action for time elapsed since checkout?

I suppose I could use Conditional Actions as a cron alternative, as I could use a commonly occurring trigger, such as "Product node is added to cart", and then go hog-wild with the custom PHP code for both the condition and the action.... run a while statement for a database query of all sales orders to check status and elapsed time, but mannnnn I don't know how to do this at all.

I know how to pull up nodes, such as...

  $query = "SELECT nid FROM {uc_products} WHERE list_price > sell_price ORDER BY model ASC";
  $result = db_query($query, $user->uid);
  while ($new_node = db_result($result)) {
    $load_this = node_load($new_node, NULL, FALSE);
    if (($load_this->status) == 1) { // run some code if the product is on sale & published }
}

But I've never played with pulling orders from the database before. Looks like I know what I'll be doing all day tomorrow Laughing out loud lol

Good night all,
BigMike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Ok guys check it out :D

Ok guys check it out Laughing out loud Smiling Laughing out loud

This will return a list of orders that have been in the Pending status for more than 24 hours:

<?php
  $result
= db_query("SELECT * FROM {uc_orders} WHERE order_status LIKE '%pending%'", $order_id);
  while (
$order = db_fetch_object($result)) {
    if ((
$order->created) < (time() + 86400)) {
      print
$order->order_id . " has been pending for more than 24 hours.<br />";
    }
  }
?>

So this just needs a bit of tweaking for use as our Condition. Thoughts on this is that we don't need to return a huge list of orders that match. We only need the condition to be TRUE if any order is > 24 hrs. So for the Condition we can just break out of the while statement when a match is found and then return TRUE. Rest assured I will post up my final code that I am using once I get this all figured out.

Now for the Action what we can do is use the while statement to run through allllllll the orders that match and change their status to a user-defined status, like "Urgent" or "Attention". Yes, if you currently have a ton of incomplete orders this will take a few seconds to accomplish the first time it runs, but after that this won't be much of a load at all.

I've never written script to write to the database, so I am going to learn how to do that next. I will keep this updated

-Mike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Ok guys check it out :D

I just discovered that time()+86400 actually equals the current time. There is a 24 hour time shift somewhere. So the new code to evaluate for "greater than 24 hours" would be time()+172800, which is two 24hr periods.

Maybe this is server specific, but this is how it is on my server.

EDIT: Wait, nevermind. I got confused. I believe the 86400 is correct. Sorry

EDIT 2: Ok, my above code is wrong. I am working on fixing it right now...

EDIT 3: Here is my final code that will pull Pending orders outside of 24 hrs:

$result = db_query("SELECT * FROM {uc_orders} WHERE order_status = 'pending'", $order_id);
while ($order = db_fetch_object($result)) {
  if ((time() - $order->created) > 86400) {
    // do something
  }
}
BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Conditional Action for time elapsed since checkout?

Here is the code to update the status of order #12345:

<?php
  db_query
("UPDATE {uc_orders} SET order_status = 'Attention' WHERE order_id = '12345'", $order->order_id);
?>

This assumes that the status 'Attention' has already been added to the Order workflow (admin/store/settings/orders/edit/workflow)

Now I am gonna try to implement everything.

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
Re: Conditional Action for time elapsed since checkout?

Ok guys, I got it. It's all done.

1) Create a new Order workflow as you see fit at admin/store/settings/orders/edit/workflow
2) Browse to admin/store/ca/add
3) Trigger: Product node is added to cart
4) Condition: No condition needed at all. The trigger is all we need.
5) Actions: "Execute custom PHP code"

Enter the following into the "Custom PHP" field, using whatever workflow name you created in step 1:

$result = db_query("SELECT order_id, created FROM {uc_orders} WHERE order_status = 'pending'", $order_id);
while ($order = db_fetch_object($result)) {
  if ((time() - $order->created) > 86400) {
    db_query("UPDATE {uc_orders} SET order_status = '###WORKFLOWNAME###' WHERE order_id = $order->order_id", $order->order_id);
  }
}

6) Save changes. Done. Now whenever any product is added to any cart, this CA will check for missed Sales Orders.

Things to consider:

This is like a poorman's poormansCAcron LOL but we have the power to do whatever we want with PHP. This "cron job" as I am calling it, will be called whenever a product is added to a cart. So you might be thinking... Hmmm that is a lot of calls to the database which will put a strain on your server. So here are some considerations to keep in mind:

1) We are only querying the Database for Pending orders. Yes, as I mentioned before, the first time you run this it will update the status on however many Pending orders you have, which may require some resources, but after this process, you are only going to have however many Pending orders you have in less than a 24 hour time period after the last visitor or user added a node to her cart. So it may only be calling a dozen or so database values, which isn't so bad.

2) Keep "cron" in mind and realize that hey, we control it as we see fit with PHP, so why not add PHP code before all of this to only run our code say every other hour (just check if the current hour is odd or even), or how about only run the code once a day between maybe 6am and 10am, assuming you'd have at least 1 visitor add at least 1 product to his cart.

3) Moreover, you could use the Trigger for "Customer completes checkout" which will be less frequent than visitors adding nodes to their carts, just remember this requires subsequent checkouts to occur within 24 hrs from one another..... if you need to be that precise with your 24 hr time period, or whatever time period you set. Be sure to change your variable name to something other than $order as this may conflict with the available PHP variable $order.

So you can see that this can be as flexible as you want. This is really cool!

Ok enough for now! CYA!
BigMike

BigMike's picture
Offline
Joined: 10/20/2008
Juice: 1057
BigMike wrote: I remember
BigMike wrote:

I remember reading somewhere that it is possible to set a CA trigger based on elapsed time since a customer completed checkout, but I've searched and searched and can't find it.

I know this is old news, but to bring more closure to this I finally found out where I read this, see this reply #3: http://drupal.org/node/649306#comment-2334330

Posted by longwave on December 3, 2009 at 10:19am:

Quote:

Orders should not stay at "pending" for long either, in most stores a Conditional Actions rule should move them to "payment received" or "completed" or another status shortly after checkout.

We are really really really glad I figured all this out. It is working out REALLY GREAT for us. Hope this info helps someone in the future. I also figured out how add classes & theme the orders page (admin/store/orders) so I can change the background-color of each row for orders depending on their current Status level. Managing orders has never been so easy for our team.

Regards,
BigMike