display a metafield or a variable from product to a page

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
Highlighted
Globetrotter
640 60 129

Could you maybe post the code that you have for your donation thing and your page currently? Right now I'm having a little trouble visualizing what you're saying. In my head I can kind of think of a way. You can assign a variable and then pass it to a snippet using "with". For instance on my site I have a product carousel snippet and I pass the collection products to make a carousel out of any collection that I want:

 

{% assign collection = collections.t-shirts.products %}
{% include 'product-carousel' with collection %}

// Then in my product-carousel snippet I have access to the collection variable

{% for product in collection limit: 10 %}
//code for carousel
{% endfor %}

// This pulls from collections.t-shirts.products

However, I cannot think of how to get something OUT of a snippet. Is product-sold-count a snippet? If so maybe you can include it on your page. Then in your product-sold-count you can use an if statement to see if the page handle is the page you want it on, and set it's display to none so you don't see anything visual on it but still have access to the variables inside of that snippet, so like:

 

//product-sold-count.liquid-------->
<style>
{% if page.handle == "your-page" %}
.your-main-container {
display: none;
}
{% endif %}
</style>
{% assign your_variable = "3" %}
// end product-sold-count.liquid ------->

//page.liquid ------>
{% if page.handle == 'your-page' %}
{{ your_variable }} //output '3' 
{% else %}
{{ page.content }}
{% endif %}

You would have to use this approach to be able to use liquid variables inside your page as you cannot use them in your WYSIWYG editor and Im pretty sure you cannot dynamically change metafileds. 

If you'd like to make any edits to your store, please send me a direct message and we can discuss what you'd like to accomplish :D
0 Likes
New Member
5 0 0

Well the code to get the total sum of the donation (this is called product-sold-count.liquid in the snippets folder):

 

Note, that this is important, that this should be only be for one specific product.

 

{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
  {% assign productInventory = product.variants.first.inventory_quantity %}
	{% assign spendenGesamt = productStartCount | minus:productInventory | times: product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
   <p>{{ spendenGesamt }} € gespendet!</p>
{% endif %}

Then I tried to access the page.liquid and I also tried to access theme.liquid to include this snipped by this code:

 

{% include 'product-sold-count' %}

But this won't work unfortunately. :(

0 Likes
Globetrotter
640 60 129

I think this doesnt work on your page.liquid because you do not have access to the product object inside page.liquid. I remember responding to something like this a long time ago and I found the topic. You may want to give Jason's suggestion a try using  "all_products" so you can access your products metafields.

 

https://community.shopify.com/c/Shopify-Design/How-can-I-access-product-metafields-outside-of-the-pr...

If you'd like to make any edits to your store, please send me a direct message and we can discuss what you'd like to accomplish :D
0 Likes