Bilonik
Bilonik

Reputation: 21

Shopify Script Tag Variables

How to get customer variable in script tag installed via admin api ?. I have try {{customer.email}} in liquid and js

Upvotes: 0

Views: 590

Answers (1)

Subhrajyoti Das
Subhrajyoti Das

Reputation: 2710

I am assuming you are loading a js via script tag. So, the liquid variable {{customer}} won't be available to you. What you can do is add a liquid code via your js which gets interpreted by Shopify and you can then use those variables. Example below:

cust_element = '{% if customer %}<span id="current-customer" data-customer-id="{{customer.id}}" data-customer-name="{{customer.name}}" data-customer-email="{{customer.email}}"></span>{% endif %}';
var html = $.parseHTML(cust_element);
var $currentCustomer = $(html).find("#current-customer");
if ($currentCustomer.length > 0) {
  user_id = $currentCustomer.data("customer-id");
  name = $currentCustomer.data("customer-name");
  email = $currentCustomer.data("customer-email");
}

Upvotes: 1

Related Questions