Advertisement
verygoodplugins

Untitled

Jul 2nd, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. function send_additional_drip_events( $order_id, $contact_id ) {
  4.  
  5.     $order = wc_get_order( $order_id );
  6.  
  7.     $events = array();
  8.  
  9.     foreach ( $order->get_items() as $item ) {
  10.  
  11.         $product_id   = $item->get_product_id();
  12.         $product_name = $item->get_name();
  13.  
  14.         $categories = get_the_terms( $product_id, 'product_cat' );
  15.  
  16.         $categories_array = array();
  17.  
  18.         foreach ( $categories as $category ) {
  19.             $categories_array[] = $category->name;
  20.         }
  21.  
  22.         $categories_string = implode( ', ', $categories_array );
  23.  
  24.         $event_expires_at = get_post_meta( $product_id, 'event_expires_at', true );
  25.  
  26.         $event_expires_at = date( 'Y-m-d', strtotime( $event_expires_at ) );
  27.  
  28.         $author_id   = get_post_field( 'post_author', $product_id );
  29.         $artist_name = get_the_author_meta( 'display_name', $author_id );
  30.  
  31.         $order_date = strtotime( get_post_meta( $order_id, '_paid_date', true ) );
  32.  
  33.         $events[] = (object) array(
  34.             'email'       => wp_fusion()->crm->get_email_from_cid( $contact_id ),
  35.             'action'      => 'Product Purchased',
  36.             'occurred_at' => date( 'c', $order_date ),
  37.             'properties'  => array(
  38.                 'product_name' => $product_name,
  39.                 'product_id'   => $product_id,
  40.                 'product_cat'  => $categories_string,
  41.                 'expires_at'   => $event_expires_at,
  42.                 'artist'       => $artist_name,
  43.             ),
  44.         );
  45.  
  46.     }
  47.  
  48.     $event_data = array( 'events' => $events );
  49.  
  50.     error_log( 'sending events' );
  51.     error_log( print_r( $event_data, true ) );
  52.  
  53.     $api_token  = wp_fusion()->settings->get( 'drip_token' );
  54.     $account_id = wp_fusion()->settings->get( 'drip_account' );
  55.  
  56.     $params = array(
  57.         'headers' => array(
  58.             'Authorization' => 'Basic ' . base64_encode( $api_token ),
  59.             'Content-Type'  => 'application/json',
  60.         ),
  61.         'body'    => json_encode( $event_data ),
  62.     );
  63.  
  64.     $request = 'https://api.getdrip.com/v2/' . $account_id . '/events/';
  65.  
  66.     $response = wp_remote_post( $request, $params );
  67.  
  68. }
  69.  
  70. add_action( 'wpf_woocommerce_payment_complete', 'send_additional_drip_events', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement