Hello,
So I’m trying to reference another product on a product page.
I have done the following:
-Created a metafield under “Product”
-This metafield I selected “Product” for the metafield type.
-Selected “Storefronts access”
I got it to show on the product page using the custom liquid block and the following code:
{{ product.metafields.custom.alternate_product }}
Problem I seem to be having: it won’t actually show a product. It throws this code in place of where the product should show:
gid://shopify/Product/7798245458109
Is there some reason why it will only show that but not the actual product?
Thank you in advance for your time
Hi @cmproduct85736 ,
To display the actual product instead of its ID, you need to fetch the product details using the product ID.
Here’s the code:
{% assign product_gid = product.metafields.custom.alternate_product %}
{% assign product_id = product_gid | split: '/' | last %}
{% assign alternate_product = all_products[product_id] %}
{% if alternate_product %}
## {{ alternate_product.title }}
View Product
{% else %}
Alternate product not found.
{% endif %}
Explanation1. Extract the product ID from the gid://shopify/Product/7798245458109 string.
- Use all_products to fetch the product details.
- Display the product’s title and link if it exists.
Thanks!
Hi @thirtycoders
Thank you for the quick reply and that code! I would have never figured that out on my own…why is Shopify putting that feature there but no way to know how to use it properly? You should be able to select that option and just use it (as a block) - no code necessary. Makes no sense.
Anyway, unfortunately it didn’t really work.
A) It only put a link? I was hoping it would maybe show the product picture, name and the price? Or at least the product name and picture?
B) The link simply refreshes the current product page that I’m on when I click the link. It does not bring me to the product I wanted.
Thank you again in advance!