I am currently trying to accumulate the order amounts of customers into a metafield. But somehow I do not manage to read the metafield value. So basically everytime a new order is payed, I just overwrite the metafield with the last order amount.
I tried just to use the update metafield function and switched to “run code” now.
But somehow it does not work. This is my current trial:
Could anybody help me, how to access the metafield value correctly?
I’m pretty sure you can do this without the Run Code portion and only using the Update Metafield action to update it.
The key is to reference the current value of the metafield within the Liquid expression for the new value. When you configure the Update Metafield action to update a `Company` metafield, the `company` object in the Liquid context refers to the company whose metafield you are updating.
So, for example, if your metafield is for `company.metafields.custom.your_metafield_key`, you’d set the new value in the “Update Metafield” action like this:
Here’s what that does:
* company.metafields.custom.your_metafield_key: This accesses the current value of the metafield before the update.
* | default: 0: If the metafield is empty or doesn’t exist yet, it defaults the value to `0` instead of `null`, preventing Liquid errors.
* | plus: order.total_price: This adds the `total_price` of the current order (which is available from your “Order Paid” trigger) to the existing value.
Make sure your metafield is set to an integer or decimal content type to handle numerical values correctly. If it’s a string, Liquid will concatenate instead of adding.
Hope that helps!
P.S. I’m building a gamified discount app called Game Gophers. It just got approved in the Shopify store and I’d love to get some early adopters using it. If you’re interested, let me know!
Thank you for your answer!
The problem is that within the “update company Metafield” section I can not access the metafield like this. At least I receive this error:
Try using the ‘add variable’
Should be order.purchasingEntity.PurchasingCompany.company.alias.value where alias is what is assigned when picking the metafield
Hi,
Thank you for your message.
It worked. To access I used:
{
“amount”: {{ order.purchasingEntity.PurchasingCompany.company.ytdSpend.value.amount
| default: 0
| plus: order.currentTotalPriceSet.shopMoney.amount }},
“currency_code”: “EUR”
}
Yeah that is something I need to verify. But I thought it shall be the last one. At least for now it seems to select the correct company to be adapted.
Now I gonna take a look into how to automatically change the asigned catalog