Dedicated to the Hydrogen framework, headless commerce, and building custom storefronts using the Storefront API.
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?
Solved! Go to the solution
This is an accepted solution.
p.s. I was asleep when answering beforehand.
if ($('body').hasClass('template-product')) { ... }
Doesn't get easier than that 😛
Add the code in your js file
It's return the page template name.
var pageTemplate = $("body").attr('class');
console.log(pageTemplate );
Thanks.
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:
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'); }
This is an accepted solution.
p.s. I was asleep when answering beforehand.
if ($('body').hasClass('template-product')) { ... }
Doesn't get easier than that 😛