Solved

How to get page id from storefront API JS?

dalv_happy
Tourist
6 0 0

I am developing an app for the marketplace and I included js file to the storefront.

Could you please help me to figure out how to get page id?

Accepted Solution (1)
KarlOffenberger
Shopify Partner
1873 184 901

This is an accepted solution.

p.s. I was asleep when answering beforehand.

 

if ($('body').hasClass('template-product')) { ... }

Doesn't get easier than that 😛

View solution in original post

Replies 4 (4)

KuldeepKumar20
Shopify Partner
279 34 62

Add the code in your js file

It's return the page template name.

 

    var pageTemplate = $("body").attr('class');
   console.log(pageTemplate );

 

Thanks.

For Design, Development and custom changes Hire Me.
If your problem solved then Like & Accept this Solution.
Email ID: kuldeep200395@gmail.com
dalv_happy
Tourist
6 0 0

If the body has few classes then I can't check page id 😞
And this method doesn't work when I navigate to thank you page.

var pageTemplate = $("body").attr('class');
if (pageTemplate === 'template-product') {
    // action
}

Example:

AflAOpJSFW9NtQqvzkFwWeWDrc54gY

KarlOffenberger
Shopify Partner
1873 184 901

Okay, but that's now just down to common JS stuff... I mean you can

 

// split your body classes in to an array and check if that contains the value
var classes = $('body').attr('class').split(' ');
if ($.inArray('template-product', classes) !== -1) {
  console.log('Yay, I am a Product Page');
}

// or simply check the substring exists
var clazz = $('body').attr('class');
if (clazz.indexOf('template-product') !== -1) {
  console.log('Yay, I am a Product Page too');
}

 

KarlOffenberger
Shopify Partner
1873 184 901

This is an accepted solution.

p.s. I was asleep when answering beforehand.

 

if ($('body').hasClass('template-product')) { ... }

Doesn't get easier than that 😛