Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add customer billing info to a MailChimp Group when they make a purchase using WooCommerce

I have looked high and low to resolve this, but with no luck. I think I am missing something quite basic as the fix sounds pretty straight forward.

I am trying to add customer billing details to a MailChimp Group.

It is for a site that sells online courses.

What I would like to happen is: User makes purchase on site and is automatically signed up for an appropriate MailChimp Group based on their purchase (i.e. User purchases Monthly Video Course, gets added to 'Monthly Video Course' MailChimp Group.)

I already have some code written, but it is not working (I'm getting an 'undefined variable' error). I am not sure if the variables/syntax is correct. I am by no means a coder.

Might someone be able to help me?

Here is the code I have (which I put in functions.php):

function pass_wp_to_mc() {
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey
require_once 'wp-content/plugins/woocommerce/classes/class-wc-checkout.php';


$api = new MCAPI($apikey);

// Grabs the WooCommerce Product IDs and associates them with the Mailchimp Group IDs - users are put into Groups based on product purchase.

if ($product_id == 42) {
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Monthly';

} elseif ($product_id == 142) {
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Weekly';

} else ($product_id == 144); 
$mailchimpGroupingId = 1;
$mailchimpGroup = 'Audio';

}

$merge_vars = array(
'FNAME' => $billing_first_name,
'LNAME'=> $billing_last_name,
'EMAIL'=> $billing_email,
'GROUPINGS'=>array(
array('id'=>$mailchimpGroupingId, 'groups'=>$mailchimpGroup),
)
);
$listId = 33833; //List ID found inside MailChimp on the page for your List
$my_email = '$email'; 
$double_optin = false; // People are automatically added in to List
$update_existing = true; // Will update users if they are already on the list


$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $double_optin, $update_existing);

if ($api->errorCode){
echo "Unable to load listSubscribe()!\n";
echo "\tCode=".$api->errorCode."\n";
echo "\tMsg=".$api->errorMessage."\n";
} else {
echo "Subscribed - look for the confirmation email!\n";
}

My questions are: Is this code correct? If so, is functions.php the place to put it? If so, how do I 'call' it and where will I put the call in - WordPress file? WooCommerce? thankyou.php? checkout.php? cart.php?

Any help is greatly appreciated - I've been trying to fix this for weeks!

UPDATE: I figured it out! firstly, the code was incorrect. Here is what worked:

require_once dirname(__FILE__).'/inc/MCAPI.class.php';
require_once dirname(__FILE__).'/inc/config.inc.php';

add_action('woocommerce_checkout_order_processed', 'get_info');

function get_info($order_id) { 
global $woocommerce;

$order = new WC_Order( $order_id );
$firstname = $order->billing_first_name;
$lastname = $order->billing_last_name;
$email = $order->billing_email;
$product_id=unserialize($order->order_custom_fields["_order_items"][0]);
$product_id=$product_id[0]['id'];

    global $apikey;
$api = new MCAPI($apikey);

if ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*) {
$mailchimpGroup = '*ENTER THE NAME OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

} elseif ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*) {
$mailchimpGroup = '*ENTER THE NAME OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

} else ($product_id == *GET THIS ID AT THE EDITING SCREEN OF YOUR PARTICULAR WOOCOMMERCE PRODUCT*); 
$mailchimpGroup = '*ENTER THE *NAME* OF YOUR MAILCHIMP GROUP (NOT THE TITLE)*';

$merge_vars = array(
                    'FNAME' => $firstname,
                    'LNAME'=> $lastname,
                    //'EMAIL'=> $email,
                    'GROUPINGS'=>array(
                        array('name'=>'*ENTER THE TITLE OF YOUR MAICHIMP GROUP (NOT THE NAME)', 'groups'=>$mailchimpGroup),
                        )
                    );
$listId = 'YOUR LIST ID HERE'; //List ID found inside MailChimp on the page for your List
$my_email = $email; 
$double_optin = false; // People are automatically added in to List
$update_existing = true;    // Will update users if they are already on the list               


$retval = $api->listSubscribe( $listId, $my_email, $merge_vars, $double_optin, $update_existing);
like image 675
Morgan Sully Avatar asked Jan 26 '13 18:01

Morgan Sully


1 Answers

There's a plugin for this now - take a look at WooChimp - MailChimp WooCommerce Integration. There are people here who do not know PHP that well so I thought this could be helpful.

Full disclosure: I'm the author of the plugin.

like image 175
sPaul Avatar answered Nov 09 '22 05:11

sPaul