简体   繁体   English

每次下 Woocommerce 订单时,如何从插件调用 PHP function?

[英]How to call a PHP function from a plugin every time a Woocommerce order is placed?

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.在 Wordpress 我安装了一个应用程序生成器插件,允许我向应用程序发送推送通知,当 email 从网站发送到 email 的当前用户地址时,它会自动发送推送通知。插件允许我手动向不同的用户角色发送自定义推送通知——这很好。

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.但问题是 - 我希望能够在每次收到新的 Woocommerce 订单时向“司机”用户角色发送自动推送通知。

Warning - I am a novice (clearly).警告 - 我是新手(显然)。

The plugin developer provided me with the function that sends the push notification, which is:插件开发人员为我提供了发送推送通知的 function,即:

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.我正在使用woocommerce_thankyou ,因此每次客户访问“谢谢”页面时 function 都会运行。

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 : 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每次发送新的 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);
            
            }
        }
    }
}

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.一些帮助将不胜感激。

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.基本上,在 WordPress 中使用驱动程序角色查询所有运行 session 的用户。 Then, iterate over these attempting to send everyone of them a notification.然后,遍历这些尝试向每个人发送通知。 Change the function like so:像这样更改 function:

/**
 * 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.您可以启用WP_DEBUGWP_DEBUG_LOG并检查哪个驱动程序帐户应该收到推送通知。 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.如果存在日志消息但您的测试驱动程序用户没有收到通知,则可能您需要进一步测试此 wpmobileapp_push 内容并联系https://wordpress.org/plugins/wpappninja/的开发人员,如果它似乎没有一直在工作。 The thing is: this function only inserts an entry in the plugin's database table.问题是:这个 function 只在插件的数据库表中插入一个条目。 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.这就是为什么我说您可能需要与插件开发人员交谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM