glezmen LiteCart Fan Alates Hungary Liige alates glezmen 9 mai 2025 13:29 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 Alates Lithuania Liige alates dodo 9 mai 2025 13:43 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 Alates Hungary Liige alates glezmen 9 mai 2025 13:54 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 Alates Lithuania Liige alates dodo 9 mai 2025 14:07 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 Alates Hungary Liige alates glezmen 9 mai 2025 14:13 Oh, I will try that, thank You!
glezmen LiteCart Fan Alates Hungary Liige alates glezmen 9 mai 2025 14:33 Thanks for the hint! It worked indeed this way: 'id': "<?php echo $product_id; ?>", 'name': "<?php echo $name; ?>",
tim Founder Alates Sweden Liige alates tim 9 mai 2025 22:17 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.