@alecrust sorry for the delay – hopefully since you had found the wiki – you may have already come across this page as well which addresses member tags too.
@alecrust also can you please expand on the requirements of your use case? The reason I’m asking is that you should be able to do segmentation or automations based on the products purchased by a customer already without using the tags. The e-commerce data is there for you to use just for that purpose.
Thanks for that wiki link @ryanhungate, somehow missed that.
Using that code I have a working solution to my problem, here’s the gist:
https://gist.github.com/AlecRust/86576ccba0358ca7c95f2bce7933cb5a
Be interested in any improvements to that rather hacky solution. If the order was available in mailchimp_user_tags it would be much easier!
I see that with a Mailchimp automation I could apply this product name tag to the user when they buy a product, but automations are not part of the free plan.
@alecrust this solution is not ideal because you’re making use of “the last order inserted” – and you should be executing a query using the user’s email address to pull the latest orders on that customer.
In that function you’ve created, you would probably want to find the user, or find the orders or that specific user.
You can use the get_user_by function, or the wc_get_orders function which can be found in the documentation on woo and wordpress and can query based on the email which is being passed into this function.
Thanks for the feedback @ryanhungate. With your wc_get_orders idea I have updated the code I’m using:
https://gist.github.com/AlecRust/86576ccba0358ca7c95f2bce7933cb5a
This gets the orders from the customer by email (docs). I don’t think I can rely on users existing as guest checkout is enabled.
This would be simpler if get_customer_by_email() existed, then I could get the latest order with $latest_order = wc_get_customer_last_order($customer_id); but this seems to work! Thanks again.
Upon closer inspection it looks like get_customer_by_email() used to exist, and when it did it just called WordPress’ get_user_by('email', $email);.
That suggests to me get_user_by() will return the WooCommerce customer even if that user didn’t create an account at checkout? Meaning I could do:
/**
* Add customer's last order product name as Mailchimp member tag on sync
*/
function product_mailchimp_tag($tags, $email)
{
$new_tags = [];
$customer = get_user_by('email', $email);
if ($customer) {
$last_order = wc_get_customer_last_order($customer->ID);
$products = $last_order->get_items();
foreach ($products as $product) {
$product_name_tag = [
'name' => 'Current ' . $product->get_name(),
'status' => 'active',
];
array_push($new_tags, $product_name_tag);
}
}
return array_merge($tags, $new_tags);
}
add_filter('mailchimp_user_tags', 'product_mailchimp_tag', 10, 2);
However, this does not work sadly (the custom tag is not added).