Add JavaScript to pages

LiteCart Fan
Tól től Hungary
Tagság ápr. 2025 óta

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
Tól től Lithuania
Tagság márc. 2016 óta

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
Tól től Hungary
Tagság ápr. 2025 óta

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
Tól től Lithuania
Tagság márc. 2016 óta

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
Tól től Hungary
Tagság ápr. 2025 óta

Oh, I will try that, thank You!

LiteCart Fan
Tól től Hungary
Tagság ápr. 2025 óta

Thanks for the hint! It worked indeed this way:

'id': "<?php echo $product_id; ?>", 
'name': "<?php echo $name; ?>",
tim
Founder
Tól től Sweden
Tagság máj. 2013 óta
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.

Te
Ez a weboldal nem használ cookie-kat és nem használ harmadik féltől származó nyomkövetési technológiát. Úgy gondoljuk, hogy jobban tudjuk csinálni, mint mások, és tényleg gondolunk az Ön magánéletére.