I’ve recently worked with a client who has a couple of online stores powered by WooCommerce. I’ve done a little work here and there with customizing WooCommerce, but this client is my first to want AWeber lists integrated.
The good news is that many WooCommerce plugins integrate nicely with AWeber, and she’s also using Gravity Forms, which make AWeber integration even more easy. For the most part, hooking up AWeber to whatever other feature we need is as easy as installing a plugin and setting it up.
However, we have a very specific need that was not satisfied by any plugins I found: there are certain products for which we would like to send out updates. The price of these products includes free updates to new versions and access to alternative formats. So we wanted to be able to sign someone up to an AWeber list for a specific product.
The AWeber API is actually very straightforward, and their cookbook code samples are great. WooCommerce is not always as clear, but debugging any issues is typically pretty easy. The gist of my solution, which hooks into the ‘woocommerce_order_status_completed’ action, is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $order = new WC_Order( $order_id ); $items = $order->get_items(); foreach($items as $product){ if(array_key_exists($product['id'], $product_lists)){ $app = new THCGaweberApp(); $list = $app->findList($name=$product_lists[$product['id']]); $subscriber = array( 'email' => $order->billing_email, 'name' => $order->billing_first_name.' '.$order->billing_last_name ); $app->addSubscriber($subscriber, $list); } } |
Hi!
I looked all over the internet to find a solution to it.
Just add the buyer to my list on Aweber, without any user interaction, checkboxes or anything.
Looks like that your code does it.
I don’t knwo exacly howo to implement it (used to code in C++, so coding is not a monster to me… but I’m not as skillful as I would like!).
Can you help me? Thanks!
Thank you for sharing this solution. It solves a very common issue.
Will an Aweber list need to have the exact name as the product for this to work? How are you connecting the product and the list?
Thank you!
Thanks for the post, gravitymktg! I normally response directly by email but I thought this might be a helpful clarification for everyone :)
My product_lists array is uses prod_id=>name as key=>value pairs where prod_id is the WooCommerce product ID and name is the AWeber list name.
There’s a lot of room for flexibility there, since you can set up product_lists to use any one of the WooCommerce product attributes as the keys. You can even adjust line 4 — instead of the PHP built-in array_key_exists function, you could do any test you like to match a product to a list, for example if the product title contains a particular word.
Very exciting to see that my solutions are useful. Thanks again for asking a great question!