chinds
chinds

Reputation: 1829

woocommerce rest api alert when new order comes in

I am working with the woocommerce rest api and PHP libray, I am simply displaying the orders on a webpage.

I would like to offer a sound alert when a new order is received.

I can deal with the sound itself later, but for testing just I need to figure how to check if a new order has arrived.

With the api calling:

$new_order_count = $connect->orders->get_count();

Simply returns an integer with the number of orders, so each time this int increases I need to do something.

I have be racking my brains around this for ages and cant figure out how I would do it.

Upvotes: 1

Views: 1472

Answers (2)

chinds
chinds

Reputation: 1829

So a cheap and nasty way to overcome my issue was to us eth html5 audio element and simply echo the element with an autoplay tag when a session is detected on page load.

Example:

PHP calling for order count:

if(isset($_SESSION['order_count'])) {
    if ($new_order_count->count > $_SESSION['order_count']) {
        $_SESSION['order_alert'] = '<div class="alert alert-dismissible alert-success"><strong>New Order(s)</strong> We have new orders team! </div>';
        $_SESSION['order_count'] = $new_order_count->count;

    } else {
        unset($_SESSION['order_alert']);  
    }
}

This check if the order_count session is set on page load, if it is it will get the order count from woocommerce and see if it is greater than the current count stored in the session, if yes it creates a new alert session and updates the current count session with the new order count.

If no new orders are detected it triggers the else statement unsetting the alert session.

Main Page:

if(isset($_SESSION['order_alert'])) {
    echo $_SESSION['order_alert'];
    echo '<audio autoplay>
         <source src="alert.mp3" type="audio/mp3" />
      </audio>';
}

Upvotes: 0

skeggse
skeggse

Reputation: 6323

You're effectively asking for input on how to detect a change in a system. There are generally two methods: polling and pushing. The little bit of a solution you've presented is similar to polling.

  • Polling: taking measurements on some interval, and comparing the new measurement against the old measurement to see if it changed.
  • Pushing requires tighter integration: the source of the measurements must tell you when something changes.

In order to poll, you need somewhere to store the old value. Since you're using PHP, you'll can either store it in a database, or store it in the page itself; it depends on what you're doing to trigger the poll (what kind of interval you're using).

One solution involves checking the order count every time the user navigates to a new page. This is the simplest solution given that you're using wordpress, but you'd need to find someplace to store the old value (old order count). The order count could be included in the page as a query parameter in every link (difficult to get right), or it could be store in a database (more involved in wordpress's guts).

You could use AJAX and a JavaScript timer or interval to poll on a time-based interval. This would be arbitrarily responsive (you could tune it to identify the new order within a specified period of time), but the repeated requests would be inefficient. Moreover, you'd need to also implement part of the first solution or risk missing a new order if the user navigates after a new order has arrived but before the polling detects it. This method simplifies storage of the old value, as you can just keep it in a JavaScript variable.

Upvotes: 2

Related Questions