Add JavaScript to pages

LiteCart Fan
From Hungary
Member since Apr 2025

Hi all,

I'm still trying to find my way in LiteCart, but definitely love it so far :)
Now I want to add JavaScript code to pages (like product, checkout, etc) which uses the variables from PHP.
For example I want to add this code to the product page:

 const prodProperties = {
           'productName': $product->name,
           'productProce': $product->price
           };
handleProduct(prodProperties);

I can see that all the variables I need are available in PHP via the $product variable, but I don't know where and how to add the JS code.
Is vMod what I should use for this, or do I need something else? :S

Thanks in advance!

Moderator
From Lithuania
Member since Mar 2016

You can add JS directly to template files without vMod. But if you want to add custom JS to the core files then you should use vMod so you don't loose it after first update.

LiteCart Fan
From Hungary
Member since Apr 2025

So You say I can add the JS code to /public_html/includes/templates/default.catalog/pages/product.inc.php for example. But how can I access PHP variables from there? Like $product->name? (I modified the original code snippet to show I need this).

And I have the same problem with vMod too :-/

Moderator
From Lithuania
Member since Mar 2016

Depends on what are you trying to do.

To output something in product page you can add JS code to:

templates / default.catalog / views / box_product.inc.php

Grabbing php variable like this:

<?php echo $name; ?>

Or any other variable that is missing using reference:

<?php echo reference::product($product_id)->name; ?>
LiteCart Fan
From Hungary
Member since Apr 2025

Oh, I will try that, thank You!

LiteCart Fan
From Hungary
Member since Apr 2025

Thanks for the hint! It worked indeed this way:

'id': "<?php echo $product_id; ?>", 
'name': "<?php echo $name; ?>",
tim
Founder
From Sweden
Member since May 2013
tim

To output JSON with PHP you can do this:

<script>
  var jsonVar = <?php echo json_encode([
    'id' => $product_id,
    'name' => $name,
  ]); ?>
</script>

That will take care of any special character encoding etc.

You
This website uses no cookies and no third party tracking technology. We think we can do better than others and really think about your privacy.