Advertisement
verygoodplugins

Untitled

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