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.
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. – Damocleswpmobileapp_woo_order()
function. – gregn3