Created
May 10, 2016 10:25
-
-
Save john-henry/879486726ca7f558300069b69bea7881 to your computer and use it in GitHub Desktop.
Update Zoo Vistor Entry with extra Meta info from Charge and subscribe to Campaign Monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
$plugin_info = array( | |
'pi_name' => 'Charge Update Fields', | |
'pi_version' => '1.1', | |
'pi_author' => 'John Henry Donovan', | |
'pi_author_url' => 'http://www.redmoonmedia/', | |
'pi_description'=> 'Update Zoo Vistor Entry with extra Meta info', | |
'pi_usage' => Charge_update_fields::usage() | |
); | |
class Charge_update_fields { | |
public $return_data = ""; | |
public function __construct() | |
{ | |
// Grab the Zoo Visitor Libraries | |
ee()->load->add_package_path(PATH_THIRD.'/zoo_visitor'); | |
ee()->load->library('zoo_visitor_lib'); | |
ee()->load->helper('zoo_visitor'); | |
// Grab the logged in User Details | |
$member_id = ee()->session->userdata('member_id'); | |
$member_email = ee()->session->userdata('email'); | |
$member_name = ee()->session->userdata('screen_name'); | |
// Query to Grab the meta info from Charge | |
$query = ee()->db->select("meta") | |
->where('member_id', $member_id) | |
->get('charge_stripe'); | |
foreach($query->result() as $row) | |
{ | |
ee()->load->library('encrypt'); | |
$meta_info = unserialize(base64_decode($row->meta)); | |
$profession = $meta_info['customer_profession']; | |
$profession_other = $meta_info['customer_profession_other']; | |
$newsletter = isset($meta_info['member_newsletter_subscribe']) ? $meta_info['member_newsletter_subscribe'] : 'No'; | |
// Manually chnage the memebr field ids here | |
$update_data = array( | |
'm_field_id_3' => $profession_other, | |
'm_field_id_2' => $profession, | |
'm_field_id_1' => $newsletter | |
); | |
ee()->db->where('member_id', $member_id); | |
ee()->db->update('member_data', $update_data); | |
} | |
// Sync function from Zoo Visitor | |
ee()->zoo_visitor_lib->sync_member_data(); | |
if($newsletter == "Yes"){ | |
// Campaign Monitor Subscription | |
require_once dirname(__FILE__) .'/libraries/createsend-php/csrest_subscribers.php'; | |
$wrap = new CS_REST_Subscribers('API Subscriber List ID', 'API Key'); | |
$result = $wrap->add(array( | |
'EmailAddress' => $member_email, | |
'Name' => $member_name, | |
// Match the keys already created in CM | |
'CustomFields' => array( | |
array( | |
'Key' => 'Profession', | |
'Value' => $profession | |
), | |
array( | |
'Key' => 'ProfessionOther', | |
'Value' => $profession_other | |
) | |
), | |
'Resubscribe' => true | |
)); | |
} | |
} | |
// -------------------------------------------------------------------- | |
/** | |
* Usage | |
* | |
* This function describes how the plugin is used. | |
* | |
* @access public | |
* @return string | |
*/ | |
public static function usage() | |
{ | |
ob_start(); ?> | |
Docs | |
{exp:charge_update_fields} | |
<?php | |
$buffer = ob_get_contents(); | |
ob_end_clean(); | |
return $buffer; | |
} | |
// END | |
} | |
/* End of file pi.charge_update_fields.php */ | |
/* Location: ./system/expressionengine/third_party/charge_update_fields/pi.charge_update_fields.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment