MailChimp Intergration With WordPress

Posted on Oct 01, 2020
<?php
  add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page' );
 
function bbloomer_conversion_tracking_thank_you_page($order_id) {

		global $current_user;
		get_currentuserinfo();

		$email = (string) $current_user->user_email;

		$user_firstname = get_user_meta( $current_user->ID, 'first_name', true );

		// get the last name of the user as a string
		$user_lastname = get_user_meta( $current_user->ID, 'last_name', true );

		$order = wc_get_order( $order_id );
		$meta_Data   = get_post_meta($order_id,'WooCommerceEventsOrderTickets',true);
		$MERGE4     =  $meta_Data[1][1]['WooCommerceEventsAttendeeCompany'];
		$MERGE3     =  $meta_Data[1][1]['WooCommerceEventsAttendeeDesignation'];
		$merge_fields = array('FNAME'=>$user_firstname,'LNAME'=>$user_lastname,'TEXT2' => $MERGE4,'TEXTYUI_3' => $MERGE3);
		$status  ='subscribed';
		$list_id = 'YOUR LIST ID';
		$api_key  = 'YOUR API KEY';
		$data = array(
		'apikey'        => $api_key,
		'email_address' => $email,
		'status'        => $status,
		'merge_fields'  => $merge_fields
		);
		

		$mch_api = curl_init(); // initialize cURL connection

		curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($data['email_address'])));
		curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
		curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
		curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
		curl_setopt($mch_api, CURLOPT_CUSTOMREQUEST, 'PUT'); // method PUT
		curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
		curl_setopt($mch_api, CURLOPT_POST, true);
		curl_setopt($mch_api, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($data) ); // send data in json

		$result = curl_exec($mch_api);
		//print_r($result);
		//exit;
		return $result;
        

	}
  
guest
0 Comments
Inline Feedbacks
View all comments