1
votes

in Wordpress I have installed an app-builder plugin that allows me to send push notifications to the app, It automatically sends push notifications when a email is sent from the website to the email address of the current user that is logged into the app and the plugin allows me to send custom push notifications to different users roles manually - this is fine.

But the problem is - I want to be able to send automatic push notifications to the 'driver' user role every time a new Woocommerce order is received.

Warning - I am a novice (clearly).

The plugin developer provided me with the function that sends the push notification, which is:

wpmobileapp_push($title, $message, $image_url, $page_url, $lang_2letters = 'all', $send_timestamp = '', $user_email = '');

And Im using woocommerce_thankyou so the function runs everytime a customer gets to the 'thank you' page.

So after a bit of investigating I have come up with the following function (which was added into my 'function.php) which 'should' check if a 'driver' user is logged in and should call the php function which sends the push notification to the drivers every time a new woocommerce is sent, but this does not work:

/**
 * Add a notification when a new woocommerce order is recieved.
 *
 */
add_action('woocommerce_thankyou', 'wpmobileapp_woo_order', 10, 1 );

function wpmobileapp_woo_order($order_id) {
    
    // Check if user is logged in.
    if ( is_user_logged_in() ) {
        // Get the user ID.
        $user_id = get_current_user_id();

        // Get the user object.
        $user_meta = get_userdata( $user_id );

        // If user_id doesn't equal zero.
        if ( 0 != $user_id ) {

            // Get all the user roles as an array.
            $user_roles = $user_meta->roles;

            // Check if the role you're interested in, is present in the array.
            if ( in_array( 'driver', $user_roles, true ) ) {
    
                       $order = new WC_Order( $order_id );
            $items = $order->get_items();
            $customer_address = $order->get_billing_address();
 
            $user_email = $user_meta->user_email;
            $image_url = '';
            $link = '';
    
            $title = "new order";
            $message = $order . ', ' . $items . ', ' . $customer_address;
    
            wpmobileapp_push($title, $message , $image_url, $link, $lang_2letters = 'all', $send_timestamp = '', $user_email);
            
            }
        }
    }
}

I have tried many different things to try and make this work myself to get it to send automatic notifications to the driver use role type every-time a new order is placed, but nothing works. Some help would be greatly appreciated.

1
The problem in your code is: when a customer places an order it checks whether this customer has the driver role (get_current_user(), 'driver' in this user's roles?). This will most likely never be true - unless a driver places an order for him/herself (in which case he doesn't need a push notification for starters). I think this requires a way more complex solution. WordPress doesn't have a concept of "active users", the best it can do is active sessions (and a session can remain active for weeks!) so you'd have to maintain a list of previously active driver users notifying one of these.Damocles
Hi, thanks for your comment! as I wanted the new order notification to be send to just the users who are role type 'driver' I thought that check for all users that are logged into the website as a 'driver' and show them the notification. But I have tried calling the PHP function without the bit where I check for user role and it still does not present the notification....Swifter
@Swifter Perhaps you could find the code that sends the push notification to all users of a specific user role manually, as you have mentioned that this works correctly. You could call that same code from the wpmobileapp_woo_order() function.gregn3
@gregn3 So with sending the push notifications manually the plugin provides a user interface for me to enter a message and select which user role type will view this. I have pasted the code which I think provides this functionality but I am not sure how I would customise it to send a pre-written notification to a specific user role... (I have attempted to use that code but I was un-successful)Swifter

1 Answers

3
votes

Okay, the best simple way of handling this is to send a push notification to all logged in driver users. Basically, query all users that have a running session in WordPress with the driver role. Then, iterate over these attempting to send everyone of them a notification. Change the function like so:

/**
 * Add a notification when a new woocommerce order is recieved.
 *
 */
add_action('woocommerce_thankyou', 'wpmobileapp_woo_order', 10, 1 );

function wpmobileapp_woo_order($order_id) {
    
    
  $order = new WC_Order( $order_id );
  $items = $order->get_items();
  $customer_address = $order->get_billing_address();
 
  $user_email = $user_meta->user_email;
  $image_url = '';
  $link = '';
    
  $title = "new order";
  $message = $order . ', ' . $items . ', ' . $customer_address;

  // get all users with role 'driver' and an active WP session:
  $drivers = get_users([
    'role' => 'driver',
    'meta_key' => 'session_tokens',
    'meta_compare' => 'EXISTS'
  ]);
  // notify each of these by push notification
  foreach ($drivers as $driver) {
    $driver_email = $driver->user_email;
    wpmobileapp_push($title, $message , $image_url, $link, $lang_2letters = 'all', $send_timestamp = '', $driver_email);
    error_log("WooCommerce Driver notification: sending push notification to account of $driver_email");
  }
}

You can enable WP_DEBUG and WP_DEBUG_LOG and check which driver account should've gotten a push notification. If a log message exists but your test driver user didn't get a notification, probably you need to test this wpmobileapp_push stuff further and contact the developer of https://wordpress.org/plugins/wpappninja/ if it doesn't seem to be working at all. The thing is: this function only inserts an entry in the plugin's database table. I'm not entirely sure it sends the push notification right away. That's why I said you might've to talk to the plugin developer.