亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每次下 Woocommerce 訂單時如何從插件調用 PHP 函數?

每次下 Woocommerce 訂單時如何從插件調用 PHP 函數?

PHP
jeck貓 2023-10-15 14:52:55
在 WordPress 中,我安裝了一個應用程序構建器插件,該插件允許我向應用程序發送推送通知,當電子郵件從網站發送到登錄應用程序的當前用戶的電子郵件地址時,它會自動發送推送通知插件允許我手動向不同的用戶角色發送自定義推送通知 - 這很好。但問題是 - 我希望每次收到新的 Woocommerce 訂單時都能夠向“司機”用戶角色發送自動推送通知。警告 - 我是一個新手(顯然)。插件開發者給我提供了發送推送通知的功能,即:wpmobileapp_push($title, $message, $image_url, $page_url, $lang_2letters = 'all', $send_timestamp = '', $user_email = '');我使用的woocommerce_thankyou功能是每次客戶進入“謝謝”頁面時都會運行該功能。因此,經過一番調查后,我想出了以下函數(已添加到我的“function.php”中),該函數“應該”檢查“驅動程序”用戶是否已登錄,并且應該調用發送推送通知的 php 函數每次發送新的 woocommerce 時都會發送給驅動程序,但這不起作用:/** * 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);                        }        }    }}我嘗試了很多不同的方法來嘗試讓這項工作自己完成,以便讓它在每次下新訂單時向司機使用角色類型發送自動通知,但沒有任何效果。一些幫助將不勝感激。
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

好的,處理此問題的最佳簡單方法是向所有登錄的驅動程序用戶發送推送通知?;旧?,查詢在 WordPress 中具有驅動程序角色的運行會話的所有用戶。然后,迭代這些嘗試向每個人發送通知。像這樣更改函數:


/**

?* 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");

? }

}

您可以啟用WP_DEBUG并WP_DEBUG_LOG檢查哪個驅動程序帳戶應該收到推送通知。如果存在日志消息,但您的測試驅動程序用戶沒有收到通知,可能您需要進一步測試此 wpmobileapp_push 內容。

查看完整回答
反對 回復 2023-10-15
  • 1 回答
  • 0 關注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號