Reputation: 73
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.
Upvotes: 1
Views: 811
Reputation: 1317
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.
Upvotes: 3