How to add custom JS to just the checkout page?
I found this tutorial, but as far as I can tell it will add JS to the entire site.
2 Answers 2
The recommended way to add js is with requireJS ! as it is explained in Magento DevDocs
app/design/frontend/Vendor/theme-name/Magento_Checkout/web/js/myfile.jsyour js functions goes here exemple :
define(['jquery'], function($){ "use strict"; return function hello() { alert('Bonjour Amir'); } });app/design/frontend/Vendor/theme-name/requirejs-config.jsvar config = { map: { '*': { checkoutjs: 'Magento_Checkout/js/myfile' } } };app/design/frontend/Vendor/theme-name/Magento_Checkout/templates/onepage.phtmlHere we call our js function otherwise, the js will not be loaded !
<script type="text/javascript"> require(['jquery', 'checkoutjs'], function(,ドル hello) { hello(); }); </script>- Clean all this folder contents
pub/staticexcept.htaccess - Clean all this folder contents
var/view_preprocessed - Clean all this folder contents
var/cache - Deploy the
static-content:php bin/magento setup:static-content:deploy -f
- Clean all this folder contents
-
Thank you, will try this out later! Is it possible to do it through a module or is it recommended to use a theme?Metal Mathematician– Metal Mathematician2018年03月04日 12:46:47 +00:00Commented Mar 4, 2018 at 12:46
-
1it is possible for the both, in your case you just want to call a js in your template, so I think it's not worth it to rewrite the module for that !2018年03月04日 12:49:53 +00:00Commented Mar 4, 2018 at 12:49
-
Hmm, the JS deploys, but no HTML elements load at all (i.e the checkout is blank). Am I doing something wrong?Metal Mathematician– Metal Mathematician2018年03月05日 21:35:31 +00:00Commented Mar 5, 2018 at 21:35
-
I don't know what exactly you did but i tested it and it works, recheck you code step by step2018年03月05日 21:37:57 +00:00Commented Mar 5, 2018 at 21:37
-
1Oh, I had to copy the original
onepage.phtmlto my theme right?Metal Mathematician– Metal Mathematician2018年03月06日 14:30:23 +00:00Commented Mar 6, 2018 at 14:30
I don't know if this is the best way, but this is how I did it:
Create a checkout_index_index.xml in app\code\vendor\module\view\frontend\layout
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<!-- Add local resources -->
<script src="vendor_module::js/checkout.js"/>
</head>
</page>
Then you can put your JS checkout.js in: app\code\vendor\module\view\frontend\web\js
-
2This is not the recommended way to add a js file in Magento 2, look at the console of your browser, you will definitely have errors or breaks some functions, you should use
requireJS2018年03月04日 10:26:30 +00:00Commented Mar 4, 2018 at 10:26 -
@Prince Yeah, I read it's not recommended, but it seems so straightforward. I don't get any errors. I am aware of
data-mage-initandx-magento-initbut I haven't been able to implement it successfully for the checkout. I would appreciate you help;)Metal Mathematician– Metal Mathematician2018年03月04日 11:03:36 +00:00Commented Mar 4, 2018 at 11:03
Explore related questions
See similar questions with these tags.