Adding Woocomerce Bought Product Tags to Mautic Users

Wordpress Code

This is a follow-up to the original post about adding Mautic tags to users based on what they view on your site.

In this post, I'm going to add (or remove) tags after check out, based on the products that the user bought.

In the previous post, I looked at ways to target users based on the pages they view on your site. That's great for follow up emails about special offers or price changes, but you may also want to remove those tags after the user has bought the product. There's nothing worse than getting an advert for something you have already bought.

Rather than using the Mautic plugin's shortcode and adding it directly into the template files, this time I'm going to use the raw Mautic img tags and use a Woocomerce filter to add the tag into the page. Feel free to adapt it to use the shortcode like the previous examples.

Woocomerce has various hooks and filters for modifying the output. One that seems particularly suitable is 'woocommerce_thankyou_order_received_text' since it modifies the text on the thank you page where we can add our Mautic tracking pixel and it also contains the order object which we can use to get information about the products that have been purchased.

If you are also tagging users with products they have viewed or tracking multiple sites then I would suggest including information about that in the tag so that you can clearly see what the tag is for when you see them in the Mautic admin.

When it comes to identifying the product, whilst using a title or slug may be clearer, they could always change in the future, so it's more future proof to use the product ID which will never change, even if it's not as convenient to use.

For viewed products, I use: sitename-product-viewed-ID
For bought products: sitename-product-bought-ID

This makes it easy to know which tag to remove if you are tracking both viewed and bought products.

The code can go into a templates functions.php file or into a custom hooks plugin file:

 * Modify text on the woocomerce receipt page.
function mysite_order_received_text( $text, $order ) {

    $all_products = $order->get_items();

    $tags = '';

    foreach ($all_products as $product){
        $tags .= 'mysite-product-bought-' . $product['product_id'] . ',-mysite-product-viewed-' . $product['product_id'] . ',';        

    $tags = rtrim($tags, ',');
    $tracking_img = '<img src="' . esc_attr($tags) . '" alt="Mautic Tags" />';

    return $text . $tracking_img;
add_filter('woocommerce_thankyou_order_received_text', 'mysite_order_received_text', 10, 2 );

You need to modify the code to change the src attribute of the image tag to point to your Mautic installation and update the tags and function name to reflect the name of your website.

What it's doing is grabbing an array of the items in the order and then looping through them and adding 2 tags for each product. One to add a tag for the bought product, and another to remove a tag for product viewed (prefixed with '-' which removes the tag). If you're not tagging viewed products then you can remove the second one.

It then trims the last comma from the string and inserts it into our tracking pixel, which is added to the end of the thank you message text.

NOTE: Adding these tags is useless unless the user is already identified. If there's a chance that the users could be anonymous then you can install the Mauticommerce plugin to add user during the checkout process. Make sure that the users go through a double opt-in before you send them any mail and make sure that you are complying with the laws in your country!

And that's it, you can now build segments of users that have viewed or bought specific products and follow up as you see fit.

This will only work when using a payment gateway that directs the user to the thank you page after payment is completed. If the gateway doesn't do that then the tags won't get added.