I have another Flow variable problem, most likely a syntax error but may be my precursor steps.
Objective:
I’ve set up a metafield ‘custom.shipping_weight_grams’ which I want to enter the product weight. Sounds easy but there are two scenarios:
product has no variants
product has variants
But the variable path for both seems the same, to get to the weight value field you have to go down the variant path. Regardless, I’ve set up this flow on product creation, then check if product.hasOnlyDefaultVariant then if yes, getproductvariantdata. Then I choose update product metafield, select my field then navigate to
product/variants/inventoryItem/measurement/weight/float
This defaults in this code for the value:
{% for variants_item in product.variants %}
{{variants_item.inventoryItem.measurement.weight.value}}
{% endfor %}
That gets an error “Value must be a stringified JSON object with a value (numeric) and unit”
I’ve tried many variations, I don’t think the first line is relevant as I’ve already pulled the variant data.
The API documentation suggests the weight field is InventoryItemMeasurement.weight so I thought this might work: {{ InventoryItemMeasurement.weight | weight }} but that doesn’t work.
Any help would be appreciated, have spent hours on this already!
Screenshots attached.
Hi there, I think you may be able to avoid that initial condition as well as the “Get product variant data” action by just using a “For each” to loop through the variants returned with the “Product created” trigger, like so:
Thanks RPiii, I meant to use the productvariant update field but accidentally used the product one. I created the decision fork a couple of steps back when checking the product.hasOnlyDefaultVariant value realising the end point needed to reflect that.
I also took out the getproductvariantdata step but eventually put it back in. The logic says although the create product trigger is singular, when you get data back for variants, it may be a single variant but the code must be written for it to be multiple variants even if the result is only one variant.
The full text of the error reads: “Value must be a stringified JSON object with a value (numeric) and unit (string from one the supported measurement units) fields.” For value: “\n 200.0\n”, Type: weight
The key elements of this error are:
a stringified JSON object with a value (numeric)
unit - a string from one of the supported measurement units
Is there another way to write this query specifically addressing those two elements?
This might be significant, why are the arrows grey in these two steps and a zero in the final arrow?
Thanks for your reply. I’ve tried your changes (I actually saw this from you in another discussion) but the same error persists. This is my ‘Update Product Metafield’ setup:
I didn’t understand the significance of the solution when you first mentioned it, I thought the fix was in the liquid for the variable field. I’ve since created metafields with json type and the flow is working as it should.