Skip to content

Instantly share code, notes, and snippets.

@AlecRust
Last active August 22, 2023 10:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlecRust/86576ccba0358ca7c95f2bce7933cb5a to your computer and use it in GitHub Desktop.
Save AlecRust/86576ccba0358ca7c95f2bce7933cb5a to your computer and use it in GitHub Desktop.
Add latest WooCommerce order product name as Mailchimp tag
/**
* 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);
@genuino2
Copy link

genuino2 commented Feb 28, 2022

In your opinion, for add a certain attribute value, is correct this code?

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) {
        // Get the product attribute first value(s)
        $size = $product->get_attribute('pa_formato');
      $product_tags = [
        'name' => strpos($size),
        'status' => 'active'
      ];
      array_push($new_tags, $product_tags);
    }
  }

  return array_merge($tags, $new_tags);
}
add_filter('mailchimp_user_tags', 'product_mailchimp_tag', 10, 2);`

@AlecRust
Copy link
Author

Looks legit to me. Needs testing!

@krizzmo1
Copy link

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