Advertisement
verygoodplugins

Untitled

Feb 5th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 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.         $order_date = strtotime(get_post_meta($order_id, '_paid_date', true));
  27.  
  28.         $time_offset = get_option( 'gmt_offset' );
  29.         $time_offset = $time_offset * 60 * 60;
  30.         $order_date -= $time_offset;
  31.  
  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.             )
  43.         );
  44.  
  45.     }
  46.  
  47.     $event_data = array( 'events' => $events );
  48.  
  49.     error_log('sending events');
  50.     error_log(print_r($event_data, true));
  51.  
  52.     $api_token  = wp_fusion()->settings->get( 'drip_token' );
  53.     $account_id = wp_fusion()->settings->get( 'drip_account' );
  54.  
  55.     $params = array(
  56.         'headers'   => array(
  57.             'Authorization' => 'Basic ' . base64_encode($api_token),
  58.             'Content-Type'  => 'application/json'
  59.         ),
  60.         'body'      => json_encode($event_data)
  61.     );
  62.  
  63.     $request = 'https://api.getdrip.com/v2/' . $account_id . '/events/';
  64.  
  65.     $response = wp_remote_post( $request, $params );
  66.  
  67. }
  68.  
  69. add_action( 'wpf_woocommerce_payment_complete', 'send_additional_drip_events', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement