Updating Customer Metafields with Integers via math function

Been wracking my brain about this and could really use some help…

Is there a way - via automation or through some shopify code (I have some coding experience, but still pretty new) - to set, increase, or decrease a customer metafield that uses integer values?

Backstory:

We offer a subscription service with pre-paid subscription intervals of monthly, 3-month, and annual in length. Monthly pays for 2 pickups (deliveries), 3-month pays for 6, and annual pays for 20.

I created a customer metafield titled “Pickups Remaining”. What I would like to have happen is when the customer places the order, an automation sets their “Pickups Remaining” metafield to an integer based on what subscription interval they are signed up for (2, 6, or 20).

Additionally, I would like an automation flow to decrement the metafield by 1 after each pickup.

Lastly, on every renewal, I would like an automation flow to increment or add to the metafield based on the interval they are signed up for.

For example:

Willow signs up for a monthly membership, so her “Pickups Remaining” metafield gets set to 2. After she picks up her first share/box, her metafield gets updated to reflect “1 Pickup Remaining”. Before she can pick up her second share, her pre-paid renewal processes, so the “Pickups Remaining” field gets updated to reflect that she now has 3 pickups remaining.

Is this possible? Any guidance in the right direction is helpful.

Hey @FishyTech

One approach would be to use the Admin API to update metafield values and also subscribe to/process webhooks when memberships are processed.

You can learn more about metafields and get hands on with a workshop here: https://shopify.dev/docs/apps/custom-data/metafields