display a metafield or a variable from product to a page

Solved
Highlighted
New Member
5 0 0

Hey there,

 

I am simply trying to display a metafield or a variable, which I coded into the product-sold-count.liquid (which I created on my own) to my page.liquid. My purpose behind this is to show a field (or rather a number), how much was donated, we created a seperate page for this and try to get the variable onto the page.

 

This works perfectly on the product.liquid - but I need to display this value on another page, so I tried to modify the page.liquid file, so I can put a custom template on the page to get  the value.

 

It is like this:

 

Product Page:

blabla

50$

donation amount: 5$ (this gets added to the total donations variable)

 

Page Page:

Total donations (here I need to get the total donations variable or rather access it)

XXX$

 

is this possible somehow with metafields or anything else?

0 Likes

Success.

Shopify Expert
9974 84 1491

So we're clear - you have a product that will have some data on it.

You want to grab Product data and show it on a Page.

 

If that's correct, look at using all_products.
https://help.shopify.com/en/themes/liquid/objects#all_products

 

For example:

{% assign someProduct = all_products['some-handle'] %}

Assuming you've saved a metafield with the namespace of foo and the key of bar:

My metafield is {{ someProduct.metafields.foo.bar }}
★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
5 0 0
Ah, this is a nice solution! Thank you! So... my last question is, can I assign a variable to a metafield? Like when my value of my variable gets updated, can I store it into a metafield? For example: {% assign value = ‚5‘ %} {% assign product.metafield.donation.value = value %}
0 Likes
Shopify Expert
9974 84 1491

No. Liquid will retrieve values, but won't save them.
To add data to a metafield you'll need to use the API, and app, or in some cases the native bulk editor.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes