Product Based Customer Tagging - mailchimp/mc-woocommerce GitHub Wiki

Welcome to the guide on using Product-Based Customer Tagging in the MailChimp for WooCommerce Integration. This guide will help you understand how to automatically tag customers based on their product purchases.

Overview

Custom Product Tags

Product-based customer tagging allows you to automatically segment your audience based on what products they purchase. This is valuable for creating targeted marketing campaigns, personalized follow-ups, and customer segmentation strategies. For example, you can tag customers who purchase specific workshops, product categories, or membership tiers, then use those tags in Mailchimp to send relevant content.

This feature uses the mailchimp_get_ecommerce_merge_tags filter hook to add custom tags when orders are synced to Mailchimp.

Implementation

Code Examples

You can implement product-based tagging using either Product IDs or SKUs, depending on your preference and store setup.

Example 1: Product ID-Based Tagging

This example uses WooCommerce product IDs to assign tags. Product IDs are simple numeric identifiers that are easiest to work with.

add_filter('mailchimp_get_ecommerce_merge_tags', 'mailchimp_ecommerce_add_product_tags', 10, 2);
function mailchimp_ecommerce_add_product_tags($merge_fields, $order) {
    $woo_order = wc_get_order($order->getId());

    // Initialize tags array
    $tags_to_add = array();

    // Loop through order items
    foreach ($woo_order->get_items() as $item_id => $item) {
        $product_id = $item->get_product_id();

        // Map product IDs to tags
        $product_tag_mapping = array(
            133 => 'ws01',  // Replace with your product ID
            130 => 'ws02',  // Replace with your product ID
            178 => 'ws03',  // Add more mappings as needed
        );

        // Check if this product should add a tag
        if (isset($product_tag_mapping[$product_id])) {
            $tags_to_add[] = $product_tag_mapping[$product_id];
        }
    }

    // If we have tags to add, update the merge fields
    if (!empty($tags_to_add)) {
        $merge_fields['TAGS'] = implode(',', array_unique($tags_to_add));
    }

    return $merge_fields;
}

Example 2: SKU-Based Tagging

This alternative uses product SKUs instead of IDs. This can be more maintainable if your SKUs are meaningful identifiers.

add_filter('mailchimp_get_ecommerce_merge_tags', 'mailchimp_ecommerce_add_product_tags_by_sku', 10, 2);
function mailchimp_ecommerce_add_product_tags_by_sku($merge_fields, $order) {
    $woo_order = wc_get_order($order->getId());

    // Initialize tags array
    $tags_to_add = array();

    // Loop through order items
    foreach ($woo_order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $sku = $product->get_sku();

        // Map SKUs to tags
        $sku_tag_mapping = array(
            'WORKSHOP-01' => 'ws01',
            'WORKSHOP-02' => 'ws02',
            'WORKSHOP-03' => 'ws03',
            // Add more mappings as needed
        );

        // Check if this SKU should add a tag
        if (isset($sku_tag_mapping[$sku])) {
            $tags_to_add[] = $sku_tag_mapping[$sku];
        }
    }

    // If we have tags to add, update the merge fields
    if (!empty($tags_to_add)) {
        $merge_fields['TAGS'] = implode(',', array_unique($tags_to_add));
    }

    return $merge_fields;
}

Setup Instructions

Adding the Code

The best practice is to add this code to a custom plugin or your theme's functions file:

  1. Custom Plugin (Recommended): Create a custom plugin file to ensure the code persists through theme updates
  2. functions.php: Alternatively, add to your child theme's functions.php file

Finding Product IDs

To find your product IDs in WooCommerce:

  1. Navigate to Products > All Products in your WordPress admin
  2. Hover over a product name in the list
  3. Look at the URL in the browser status bar - the ID appears as post=133
  4. Alternatively, edit a product and check the URL: post.php?post=133&action=edit

Mailchimp Configuration

You must have a TAGS merge field configured in your Mailchimp audience:

  1. Log into Mailchimp and go to your audience
  2. Navigate to Settings > Audience fields and |MERGE| tags
  3. Create a new field named TAGS (text field type)
  4. The merge tag must be exactly TAGS to match the code examples

Testing

To test your implementation:

  1. Place a test order on your WooCommerce store that includes a mapped product
  2. Wait for the order to sync to Mailchimp (or trigger a manual sync)
  3. Check the customer record in Mailchimp
  4. Verify the tag appears in the TAGS merge field

You can view merge field values in Mailchimp by:

  • Opening the contact's profile in your audience
  • Checking the merge fields section for the TAGS field value

Notes

Important Considerations

  • Timing: Tags are applied when orders are synced to Mailchimp. This typically happens on order placement, but timing depends on your queue processing configuration.
  • Multiple Products: If an order contains multiple mapped products, all corresponding tags will be added (comma-separated).
  • Tag Format: Keep tags lowercase and simple. Avoid special characters or spaces.
  • Dependencies: This code requires both WooCommerce and Mailchimp for WooCommerce to be active.
  • Order Status: Consider whether you want tags applied on all orders or only completed orders. You may need to modify the code to check $woo_order->get_status() if needed.

Advanced Usage

You can extend this concept to:

  • Tag based on product categories: $product->get_category_ids()
  • Tag based on order total: $woo_order->get_total()
  • Tag based on product variations or attributes
  • Apply different tags for different quantities purchased

For more information on working with WooCommerce orders and products, see the WooCommerce documentation.