skip to Main Content

I’m trying to create a Shopify’s dynamic global variable from one of my custom pages AND that has to be usable in a notification email template such as {{ customer.first_name }}.

So when a client visits the page, it should automatically creates or changes the variable’s value depending on the client.

I have seen these answers below from research so far,

  1. Assigning value by using liquid’s assign .

eg)

{% assign global_variable %}

problem) This only works on the same page.

  1. Setting the variable on settings_schema.json file.

eg)

{
"type": "text",
"id": "global_variable",
"label": "global variable",
"default": "Variable value"
}

prob) This works globally in all liquid files, but not in the Shopify’s notification email templates.

  1. using metafield.

eg)

document.addEventListener("DOMContentLoaded", () => {
$.ajax({
    type: 'POST',
    url: '/admin/api/2020-04/metafields.json',
    namespace: "inventory",
    key: "testing",
    value: "testingtesitng",
    value_type: "string",
    success: function(data) { 
      console.log(data)
    },
    error: function(XMLHttpRequest, textStatus) {
      Shopify.onError(XMLHttpRequest, textStatus);
    }
});

prob) I don’t know if that’s a correct POST call, but somehow I get a HTML code as response and it does not work either on any liquid file or email template.

Long story short, I just want to know if it’s possible to create a variable that can be used in notification email templates like customer.first_name or shop.global_variable etc.

I would really appreciate if any help !!

Thanks

3

Answers


  1. The Shopify theme and email template/notifications are different platforms, they don’t share variables.

    You can think it like a website and a mailchimp, they don’t share the same code, so you can’t access for example a JS variable from a site inside the MailChimp template.

    The same logic applies here, Shopify email templates are scoped for themselves, they don’t share variables between themselves or between the site.


    So long story short, no you can’t create a global variable in the Shopify theme and pass it to the notification email.

    Login or Signup to reply.
  2. I think you should use metafields. After creating a metafield, you can retrieve it on the template liquid file easily.

    After you create the metafield, you can try if it is stored with the metafield get request. Just post the link on browser to see if it’s correct.

    If it’s correct, you can load it on the liquid file. Personally, I use metafield to save custom meta tags and load it to liquid. It works very well

    Login or Signup to reply.
  3. Let me highlight some key points:

    Theme Customization:

    • Using theme customization we can not set a global variable which may be accessible in the Shopify Email template(s).

    Custom APP:

    • If you are familiar with Shopify APP development then we can do a trick.
    1. Shopify give us "shop.metafields". You can add data in "shop.metafields" using the end point "/admin/metafields.json" Shopify API

    2. After adding the data you can access the metafields in the email template by using following simple liquid syntax

      {{ shop.metafields }}

    FYI – I did that before for myself

    Note: You can not call the API without APP or using AJAX call at theme level

    All the best

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search