Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function send_additional_drip_events( $order_id, $contact_id ) {
- $order = wc_get_order( $order_id );
- $events = array();
- foreach ( $order->get_items() as $item ) {
- $product_id = $item->get_product_id();
- $product_name = $item->get_name();
- $categories = get_the_terms( $product_id, 'product_cat' );
- $categories_array = array();
- foreach ( $categories as $category ) {
- $categories_array[] = $category->name;
- }
- $categories_string = implode( ', ', $categories_array );
- $event_expires_at = get_post_meta( $product_id, 'event_expires_at', true );
- $event_expires_at = date( 'Y-m-d', strtotime( $event_expires_at ) );
- $author_id = get_post_field( 'post_author', $product_id );
- $artist_name = get_the_author_meta( 'display_name', $author_id );
- $order_date = strtotime( get_post_meta( $order_id, '_paid_date', true ) );
- $events[] = (object) array(
- 'email' => wp_fusion()->crm->get_email_from_cid( $contact_id ),
- 'action' => 'Product Purchased',
- 'occurred_at' => date( 'c', $order_date ),
- 'properties' => array(
- 'product_name' => $product_name,
- 'product_id' => $product_id,
- 'product_cat' => $categories_string,
- 'expires_at' => $event_expires_at,
- 'artist' => $artist_name,
- ),
- );
- }
- $event_data = array( 'events' => $events );
- error_log( 'sending events' );
- error_log( print_r( $event_data, true ) );
- $api_token = wp_fusion()->settings->get( 'drip_token' );
- $account_id = wp_fusion()->settings->get( 'drip_account' );
- $params = array(
- 'headers' => array(
- 'Authorization' => 'Basic ' . base64_encode( $api_token ),
- 'Content-Type' => 'application/json',
- ),
- 'body' => json_encode( $event_data ),
- );
- $request = 'https://api.getdrip.com/v2/' . $account_id . '/events/';
- $response = wp_remote_post( $request, $params );
- }
- add_action( 'wpf_woocommerce_payment_complete', 'send_additional_drip_events', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement