glezmen LiteCart Fan From Hungary Member since Apr 2025 glezmen May 9 2025 01:29 PM 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!
dodo Moderator From Lithuania Member since Mar 2016 dodo May 9 2025 01:43 PM 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.
glezmen LiteCart Fan From Hungary Member since Apr 2025 glezmen May 9 2025 01:54 PM 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 :-/
dodo Moderator From Lithuania Member since Mar 2016 dodo May 9 2025 02:07 PM 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; ?>
glezmen LiteCart Fan From Hungary Member since Apr 2025 glezmen May 9 2025 02:13 PM Oh, I will try that, thank You!
glezmen LiteCart Fan From Hungary Member since Apr 2025 glezmen May 9 2025 02:33 PM 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 May 9 2025 10:17 PM 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.