Last active
August 22, 2023 10:53
-
-
Save AlecRust/86576ccba0358ca7c95f2bce7933cb5a to your computer and use it in GitHub Desktop.
Add latest WooCommerce order product name as Mailchimp tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add customer's last order product name as Mailchimp member tag on sync | |
*/ | |
function product_mailchimp_tag($tags, $email) | |
{ | |
$new_tags = []; | |
$query = new WC_Order_Query(); | |
$query->set('customer', $email); | |
$customer_orders = $query->get_orders(); | |
if ($customer_orders) { | |
$last_order = $customer_orders[0]; | |
$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); |
Looks legit to me. Needs testing!
This is exactly what I'm looking for. I added this code to my child theme's functions.php But it's not working. Any idea if this code still works with current versions of WooCommerce and MailChimp?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your opinion, for add a certain attribute value, is correct this code?