0

Due to terrible management I'm currently building an ecommerce from scratch. I got to the point where I need to do some lightweight stock management, and I figured that after the user sets their intention to buy something (proceed to check-out from cart), I should diminish the stock amount and create an order in the DB with pending status. However, I want to check if, after one day of this order being created, the status is still pending. If it is, then I should return the stock.

This would be easy if PHP had a function like JavaScript's setTimeout(), but I'm finding it difficult to find similar functionality. I thought about creating a cron job, but I don't know how I could make it so that the executed script got all the necessary parameters to perform the status check (most importantly, the order ID).

Are there any other options?

JS Rolón
  • 273
  • 1
  • 9
  • Running on Linux, then use a crontab entry to run a job at a given time. read about cron service on linux http://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – JOUM Nov 19 '16 at 07:44
  • depending upon the database you could probably do that completely in the db ( mysql - events ? ) – Professor Abronsius Nov 19 '16 at 07:51

1 Answers1

1

As you know, PHP is interpreted language, so once started execution, it will run until the program is ended or the maximum time limit ( As per your PHP settings ) is reached. So in a single script it wont be easy.

There are two options

  1. Put maximum execution time limit 0 (ie. no time limit) in your config and do a PHP script which runs an infinite loop and check your database and calling your status checking and status change functions. But this will be resource intensive as it is an infinite loop and having 1000s of orders will create crashes .
  2. Use cronjobs -> Using cronjobs, you can execute a script file in a fixed interval of time. You have to create two cron job scripts.

    1. A script that checks the status of each order and push those with pending status to an intermediate table which stores id of those with pending status. That means

       foreach($orders as $order) {
          if($order->status=='pending' && $order->date<CURRENT_DATE)
              push_order_to_cancellable_orders_table();
       } 
      
  3. A script that cancels the cancellable orders and remove those from cancellable orders table.ie.

    $cancellable_orders = get_items_from_cancellable_orders_table();
    foreach($cancellable_orders as $order) {
        cancel_order($order);
        remove_from_cancellable_orders($order);
    }
    

Run each of this scripts in two separate cron jobs and set a small interval such as 1 minute for the crons.
Again, the two actions in the second script can be combined in to single action in a MYSQL stored procedure and you can call that procedure, which will be more fast.

But still , the more efficient and faster way will be by using MYSQL events and procedures.

Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90