glezmen LiteCart Fan Fra Hungary Medlem siden apr. 2025 glezmen mai 9 2025 01:29 p.m. 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 Fra Lithuania Medlem siden mar. 2016 dodo mai 9 2025 01:43 p.m. 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 Fra Hungary Medlem siden apr. 2025 glezmen mai 9 2025 01:54 p.m. 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 Fra Lithuania Medlem siden mar. 2016 dodo mai 9 2025 02:07 p.m. 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 Fra Hungary Medlem siden apr. 2025 glezmen mai 9 2025 02:13 p.m. Oh, I will try that, thank You!
glezmen LiteCart Fan Fra Hungary Medlem siden apr. 2025 glezmen mai 9 2025 02:33 p.m. Thanks for the hint! It worked indeed this way: 'id': "<?php echo $product_id; ?>", 'name': "<?php echo $name; ?>",
tim Founder Fra Sweden Medlem siden mai 2013 tim mai 9 2025 10:17 p.m. 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.