php - 每次下 Woocommerce 订单时,如何从插件中调用 PHP 函数?
问题描述
在 Wordpress 中,我安装了一个 app-builder 插件,允许我向应用程序发送推送通知,当电子邮件从网站发送到登录到应用程序的当前用户的电子邮件地址时,它会自动发送推送通知,并且插件允许我手动向不同的用户角色发送自定义推送通知——这很好。
但问题是 - 我希望能够在每次收到新的 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);
}
}
}
}
我已经尝试了许多不同的方法来尝试自己完成这项工作,以使其在每次下新订单时向驾驶员使用角色类型发送自动通知,但没有任何效果。一些帮助将不胜感激。
解决方案
好的,处理此问题的最佳简单方法是向所有登录的驱动程序用户发送推送通知。基本上,查询所有在 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 内容并联系https://wordpress.org/plugins/wpappninja/的开发人员,如果它似乎没有一直在工作。问题是:这个函数只在插件的数据库表中插入一个条目。我不完全确定它会立即发送推送通知。这就是为什么我说您可能需要与插件开发人员交谈。
推荐阅读
- caching - 如何减少对服务器端认证授权平台的访问
- python-3.x - 我如何获得成员在 discord.py 中输入的命令
- regex - 如何在飞镖中用骆驼案来判案
- nebular - 如何在 ng2-smart-table 中动态添加自定义图标/css
- postgresql - 使用 postgres 超级用户创建 Postgres FDW,但对远程模式具有只读访问权限
- javascript - 导航栏下方的 Bootstrap 品牌图像重叠容器
- excel - 是否有一种简单的方法可以使用每天更新的 excel 文件来更新 PowerBI 仪表板?
- laravel - Laravel关系多对多过滤枢轴字段
- r - 如何在ubuntu中导入rstudio安装
- python - 使用 OpenCV 处理 Hololens2 实时预览帧