# Automating bulk invite

**URL:** <https://meta.discourse.org/t/automating-bulk-invite/164259>\
**Category:** Product Support\
**Created:** [September 16, 2020, 5:57pm UTC](https://meta.discourse.org/t/automating-bulk-invite/164259 "2020-09-16T17:57:30Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![yaxu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yaxu/32/178942_2.png) [@yaxu](https://meta.discourse.org/u/yaxu)\
**Post date:** [September 24, 2020, 8:23am UTC](https://meta.discourse.org/t/automating-bulk-invite/164259/4 "2020-09-24T08:23:46Z")

</div>

Hi thanks for looking, I managed to hack together something with the documented API (with emphasis on hack, but maybe this is useful for someone else):

```php
function tidal_process_order( $order_id ){
        $order = new WC_Order($order_id);
        $order_email = $order->get_billing_email();
        $group_names_arr = [];
        // get skus as group names
        foreach ( $order->get_items() as $item ) {
          if ( $item['product_id'] > 0 ) {
            $_product = $item->get_product();
            $sku = $_product->get_sku();
            array_push($group_names_arr, $sku);
          }
        }

        $group_names = join(",",$group_names_arr);

        // check if discourse user already exists with email
        $r=wp_remote_get( "https://club.tidalcycles.org/admin/users/list/all.json?email=$order_email", array(
                          'headers' => array('Api-key' => 'redacted',
                                             'Api-Username' => 'redacted')
                                            )
                        );
        $matches = json_decode($r['body']);
        if (count($matches) > 0) {
          // user exists, add them to groups
          $user_id = $matches[0]->id;
          foreach($group_names_arr as $group) {
            // get group id from group name
            $r=wp_remote_get( "https://club.tidalcycles.org/groups/$group.json", array(
                              'headers' => array('Api-key' => 'redacted',
                                                 'Api-Username' => 'redacted')
                              )
                            );
            $group = json_decode($r['body']);
            $body = json_encode(array('group_id' => $group->group->id));
            // add user to group
            $r=wp_remote_post( "https://club.tidalcycles.org/admin/users/$user_id/groups", array(
                               'headers' => array('Api-key' => 'redacted',
                                                  'Api-Username' => 'redacted',
                                                  'Content-Type' => 'application/json', 
                                                 ),
                               'body' => $body
                            ));
          }
        }
        else {
            // user doesn't exist, invite them with group names
            $body = json_encode(array('email' => $order_email, 'group_names' => $group_names, "custom_message" => "Welcome!"));
            $r=wp_remote_post( "https://club.tidalcycles.org/invites", array(
                               'headers' => array('Api-key' => 'redacted',
                                                  'Api-Username' => 'redacted',
                                                  'Content-Type' => 'application/json', 
                                                 ),
                               'body' => $body
                            ));
        };
}

add_action( 'woocommerce_order_status_processing', 'tidal_process_order' );

```

---

_[View the full topic](https://meta.discourse.org/t/automating-bulk-invite/164259)._
