dynamic url in metafield type 'url'

dynamic url in metafield type 'url'

sfusobuono
Tourist
12 0 3

hi all, i have a 'url' metafield type in which i'd need to make 'dynamic'

 

This metafield type requires a https:// scheme and the original value is like

 

https://domain.com/pages/xyz

I have 2 countries and languages and i'd prefer to not translate in the translate and adapt every single url but making this 'dynamic' like

 

https://domain.com/dynamicCountry/dynamicLanguage/pages/xyz

 

or even just 

 

https://dynamicdomain%country&language/pages/xyz

 

the theme is already configured to recognize country and language so my problem here is just that the URL type metafield requires a https:// start and that isn't allowing me to add only the /pages/xyz that would inheritate the domain/country/language as done in other page.

 

any thoughts? thanks in advance

 

 

 

Replies 3 (3)

01tranali
Shopify Partner
53 3 5

Hi,

 

Try below-

{% assign country = shop.locale %} 
{% assign language = shop.language %} 

<!-- dynamic URL -->
{% assign dynamic_url = "https://domain.com/" | append: shop.locale | append: "/" | append: shop.language | append: "/pages/xyz" %}
<a href="{{ dynamic_url }}">Dynamic url page</a>

 

 

Thanks,

T

Best,
T

sfusobuono
Tourist
12 0 3

thanks! this seems a code for the theme, but i'd need to add this in a 'url' metafield placeholder - wdyt?

Joanet1
Shopify Partner
9 2 1

i think you can use Metafields API for that. You'll need a small app or a serverless function to do this. The process would be:

  1. When a product (or whatever object the metafield is attached to) is created or updated, your app/function would trigger
  2. It would retrieve the country and language (likely from the request.locale if your app is theme-based or from other context if it's external)
    It would construct the full URL: https://{{domain}}/{{country}}/{{language}}/{{page_path}} (where page_path comes from wherever you're storing that part)
  3. It would then use the Metafields API to update the metafield with this dynamically generated URL

hope that helps!