Counting Product Type Purchases a Customer has Made

My store sells a number of different types of products: hats, shirts, socks, etc. I have a metafield set for my customers called “Hats Purchased”. It is an integer type. My goal is for this metafield to display how many hats a customer has purchased all-time and automatically update after every purchase. I built a simple workflow to accomplish this, but it does not work.

What am I doing wrong?

What does “not work”?

You are inserting the customer id into that value, which will look something like gid://shopify/Customer/47474378. That is a string as adding a “1” doesn’t really make sense.

I think you want the previous value of that same metafield. To add that, you would select “Add a variable” and choose “Order / Customer / metafield” and then choose the specific metafield you want to insert. Once you do that, it will add the metafield to the workflow and you’ll be able to choose the “Value” to insert. Then you add your “| plus: 1” to that.

Thanks for replying Paul.

I can see how it doesn’t make sense, but what I screenshotted is what a previous Shopify help rep through chat had come up with that partially worked. It will populate the correct metafield, but it will never go higher than 1.

The way I understand it is in the flow that I have it looks for “a paid order” >>> then checks if their are any products in that order that are in my “Headwear” category >>> then checks to see that a customer id exists >>> if it does exist it should update the chosen metafield (which I have defined as “custom.hats_purchased” (this is where it gets fishy) by 1 for every product purchased that is in my hat category.

It seems fishy and non-sensical cause it says literally in the final step, “Do this… Update customer metafield Add this metafield to the customer: custom.hats_purchased”

Reading that literally, it seems like it wants to add a new metafield, and that’s not what I want. I need the existing metafield to increase. But I’m lost, cause if I change the value field in the last step to just 1 or {{1}} it will work the same way as having the {{order.customer.id | plus: 1}} — if I change it to plus:1 or | plus:1 i get a syntax error when running the flow that says Exception: Got error updating metafield: “Value must be an integer.” For value: “plus:1”, Type: number_integer

I do not fully understand your direction though. You say "to add that, I would select ‘add a variable’ (where do I do this exactly?)

After replying I tried something else which I think might be what you were trying to tell me to do but with the settings seen in the screenshot here I’m still getting this error: Exception: Got error updating metafield: “Value must be an integer.” For value: “| plus: 1”, Type: number_integer

![Screen Shot 2025-02-27 at 10.57.19 AM.png|1022x805](upload://kuEOOVOpCfuJOGtQZ8T5ECpCw5p.png)

I have no idea what you are doing here. Your namespace should not be the value of the field.

Your value just has the string “| plus: 1” in it. How do you expect that to work?

Your namespace is probably “custom”. You value should be something like {{ order.customer.hatsPurchased | plus: 1 }}.

Please do review metafields and liquid a bit. If you don’t understand these basics you can do damage in your shop. Here is a doc on liquid;

https://shopify.github.io/liquid/

I don’t know either, which is why I’m here cause talking with the chat support gets me nowhere.

Now, I had the namespace as custom in my original flow, with key as hats_purchased. But the value, I guess, is where the problem is. Trying the value you suggested gives me the following error: The ability to directly output objects is no longer supported. In order to make changes to this workflow, please specify the fields you need to use.

In the value field, click “Add a variable” and chose the metafield from your environment. If it’s just single value, it will have “.value” on the end. Once you have the right variable, add that “plus” part that I showed above. Metafields are custom to your shop, so I can’t really guess the exact syntax.

Thank you for your help. This finally works!