Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active March 16, 2021 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottopolis/518f016dbe09894e7e1b7fdd9157dba9 to your computer and use it in GitHub Desktop.
Save scottopolis/518f016dbe09894e7e1b7fdd9157dba9 to your computer and use it in GitHub Desktop.
WooCommerce Abandoned Checkout Flow with MailChimp
<?php
// Add this code to a custom plugin on your site to add customer emails to your abandoned checkout flow
// Requires the free plugin code here: https://hollerwp.com/woo-checkout-save-email/
// You need to install this first: composer require drewm/mailchimp-api
// see https://github.com/drewm/mailchimp-api
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1'); // change this to your MC API key
// this action fires when a new email is added to your checkout
add_action('sb_woo_checkout_email', 'sb_woo_save_email' );
function sb_woo_save_email( $email ) {
// add the user to an email list for abandoned carts. for example, send them an email a day later with a coupon.
sb_subscribe_mc( $email );
}
// if the user completes checkout, remove them from the cart abandonment flow
add_action( 'woocommerce_new_order', 'sb_abandonment_unsubscribe' );
function sb_abandonment_unsubscribe( $order_id ){
$order = wc_get_order( $order_id );
$user = $order->get_user();
if( $user ){
// remove this person from your abandoned cart email flow here
sb_delete_from_mc( $user->user_email );
}
}
// subscribe customer to our MailChimp list
sb_subscribe_mc( $email ) {
// change to your list ID
$list_id = 'b1234346';
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
}
// unsubscribe from abandoned checkout list
sb_delete_from_mc( $email ) {
// change to your list ID
$list_id = 'b1234346';
$subscriber_hash = MailChimp::subscriberHash( $email );
$MailChimp->delete("lists/$list_id/members/$subscriber_hash");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment