Cart66 Action Hook – cart66_after_order_saved

We’ve recently been working on a horoscopes website for one of our clients that has some content that is for members only. The Cart66 WordPress eCommerce has been great for handling the PayPal subscriptions but I had to add some custom functionality so that new members on certain levels would be automatically signed up to a mailing list which is handled by Constant Contact.

Cart66 actually has Constant Contact integration which adds an “opt-in” style mailing list signup that is added to the checkout which would normally work fine for most eCommerce sites however I we needed to make the signup compulsory for the user and to do it seamlessly without the user knowing that this has occurred so they receive the daily emails that they have purchased.

The undocumented cart66_after_order_saved hook

I hunted around the Cart66 support section to see if Cart66 had any hooks built in and I found out that there are four hooks which aren’t documented at this stage. One of these hooks is the cart66_after_order_saved hook which I decided to use for the additional functionality I required. I started with a function and after hunting through the Cart66 code I found that the $orderInfo array would get passed into the function.

function sennza_add_to_constant_contact($orderInfo){

/* This is where my logic will go*/

add_action( 'cart66_after_order_saved' , 'sennza_add_to_constant_contact' );

What’s in the $orderInfo array?

Next up I need to find out what the contents of the $orderInfo array are so I used print_r($orderInfo) to echo them out. The array contents are as follows:

[ship_first_name] => Test
[ship_last_name] => User
[ship_address] => 888 Brunswick Street
[ship_address2] =>
[ship_city] => NEW FARM
[ship_state] => Queensland
[ship_zip] => 4005
[ship_country] => Australia
[bill_first_name] => Test
[bill_last_name] => User
[bill_address] =>
[bill_address2] =>
[bill_city] =>
[bill_state] =>
[bill_zip] =>
[phone] =>
[email] =>
[coupon] => none
[tax] =>
[shipping] => 0.00
[subtotal] => 0
[total] => 0.00
[non_subscription_total] => 0.00
[trans_id] => MT-ZLPLDQNSJC7JGE
[status] => New
[ordered_on] => 2012-01-31 13:06:05
[shipping_method] => None
[account_id] => 2829
[ip] =>
[discount_amount] => 0
[id] => 4987

 Alright, stop… Database time!

Now I knew that $orderInfo[account_id] was the newly generated Cart66 Account ID I looked into the database tables in phpmyadmin and realised that I needed to use this id to find out the membership level (or ‘feature_level’ as Cart66 names it).

function sennza_add_to_constant_contact($orderInfo){
global $wpdb;;
$account_id = $orderInfo[account_id];
$table_name = $wpdb->prefix . "cart66_account_subscriptions";
$sql = "SELECT `feature_level` FROM $table_name WHERE `account_id` = $account_id";
$account_type = $wpdb->get_var($sql);

/* User $account_type to perform the Constant Contact Logic */

add_action( 'cart66_after_order_saved' , 'sennza_add_to_constant_contact' );

Take note of the $wpdb class that I’ve interacted with in this function. The $wpdb class has a massive amount of power that makes interacting with the WordPress database a breeze! In this case I only wanted to pull out one variable so I’ve used the $wpdb->get->var function.

After that I ended up reading some of the Constant Contact API documentation to generate some XML to send to Constant Contact so that the subscribers were instantly added to the appropriate mailing list.

NB: I haven’t included the additional logic for the Constant Contact API as it is very site specific and I just wanted to document the data that the cart66_after_order_saved hook receives for other Cart66 developers 🙂