Re: How do i get MetaObject values on single MetaObject Page

How do i get MetaObject values on single MetaObject Page

ma92
Shopify Partner
2 0 3

I am using metaObjects in shopify. I have created a metaObject name "Projects" . I created a page "Projects" to display those saved projects through liquid code. It is working as per the requirment on Projects page but now i want to display each project detail on single page. How do i approach this ? Like should i create a page and set a template on it and in that template , fetch the metaobject handle to display its details . I am facing issue like how do i call the same single page for all projects details and display its details. Can someone please guide what is the possible way to get each metaObject values on single project page .

Thanks

Replies 12 (12)

defaizan
Shopify Partner
2 0 1

Hey @ma92, have you found any solution for this ?

dock3r
Shopify Partner
4 0 2

I am searching for the exact same thing. Have you got anything?

m1ch43l
Shopify Partner
8 0 1

me2me2me2 - any answer or hint much appreciated

Alan15
Shopify Partner
148 27 72

Any ideas on this?

Need more help? Contact me here
Alan15
Shopify Partner
148 27 72

Hi @m1ch43l , thanks for the reply.

I can't open that link. Could you paste the text in here or check the link?

Need more help? Contact me here
m1ch43l
Shopify Partner
8 0 1

Ok I see, so first I wanna make sure this solution is coming from Dock3r (https://community.shopify.com/c/user/viewprofilepage/user-id/1442260)

 

and I appreciate a lot his solution. It helped me to have it solved. I hope Dock3r is good with posting his solution in here

 

Quoting from here on from my conversation with Dock3r:

Hi,

 

The stackoverflow article takes some time to write but I think you are in a hurry.

 

I have a meta object called iro (it means writer in my language). Every product has one or more writer metaobject related to it.

 

You will find a couple of console.log lines for debug purposes. Just ignore and delete them If you don't need anymore.

 

Single page of writer

 

 

<script>console.log({{ request.path | split: "/" | last | json }});</script>

{% assign writer_handle_url = request.path | split: "/" | last %}
{% assign writers = shop.metaobjects.iro.values %}
{% assign writer = shop.metaobjects.iro.values | where: 'handle', writer_handle %}

{% for a_writer in writers %}
<script>console.log({{ a_writer.system.handle | json }});</script>
  {% if a_writer.system.handle == writer_handle_url %}
    {% assign writer = a_writer %}
    {% break %}
  {% endif %}
{% endfor %}

<script>console.log({{ writers | json }});</script>


<div class="designer-container">
  <div class="designer-body">
      <h2 class="h1">{{ writer.nev }}</h2>
      <p class="designer-text">{{ writer.leiras | metafield_tag }}</p>
  </div>
</div>

 

 

List page of writers

 

<script>console.log({{ shop.metaobjects.iro.values | json }});</script>

{%  for writer in shop.metaobjects.iro.values  %}

<script>console.log({{ writer | json }});</script>
<script>console.log({{ writer.system.handle | json }});</script>
  
<div class="designer-container">
  <div class="designer-body">
      <h2 class="h1">{{ writer.nev }}</h2>
      <a href="/pages/iro/{{ writer.system.handle }}" class="button button--primary">Bővebben az íróról</a>
  </div>
</div>
{% endfor %}

 

Alan15
Shopify Partner
148 27 72

Thanks @m1ch43l and @dock3r .

Does this solve the issue of having to create multiple single writer pages. If it does how do you create the template to display the "Single page of writer" code? Sorry if I am missing something obvious here

Need more help? Contact me here
anhdd-kuro-159
Shopify Partner
14 0 6

I tried and succeeded!

Here's my solution
- Assume we have a metaobject called movies , create some entries

Create a file named 'page.movies.liquid' in the templates directory
Now, let's create a page called 'Movies' and set its template to 'movies'

Finally, add the following code into the 'page.movies.liquid' file

 

 

{% section 'movies-in-collection' %}

{% assign movie_handle = request.path | split: '/' | last %}

{% assign movie = shop.metaobjects.movies[movie_handle] %}

<script>
console.log({{ shop.metaobjects.movies.values | json }})
console.log({{ movie | json }})
</script>
<p>Handle {{ movie_handle }}</p>
<h2>
{{ movie.title }}
</h2>

 

references: 
request object

get metaobject by it handle

 

I hope this helps!

Alan15
Shopify Partner
148 27 72

This is solved for me now by Shopify. They have introduced Metaobject page templates, you can create a template to display your different metaobject entries: https://shopify.dev/docs/themes/architecture/templates/metaobject

Need more help? Contact me here
rdutton
Shopify Partner
4 0 0

In addition, I think it's worth mentioning that to reference the metaobject associated with the page using liquid, one can use:

{{ metaobject.field_name }}

Whilst obvious once understood, for some reason does not appear to be clearly stated anywhere.

dock3r
Shopify Partner
4 0 2

It's okay @m1ch43l . Thanks for posting it here. I was originally going to write a more illustrative example but unfortunately I never got around to it.