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.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityHow 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.
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.js
your js functions goes here exemple :
define(['jquery'], function($){
"use strict";
return function hello()
{
alert('Bonjour Amir');
}
});
app/design/frontend/Vendor/theme-name/requirejs-config.js
var config = {
map: {
'*': {
checkoutjs: 'Magento_Checkout/js/myfile'
}
}
};
app/design/frontend/Vendor/theme-name/Magento_Checkout/templates/onepage.phtml
Here we call our js function otherwise, the js will not be loaded !
<script type="text/javascript">
require(['jquery', 'checkoutjs'], function($, hello) {
hello();
});
</script>
pub/static
except .htaccess
var/view_preprocessed
var/cache
static-content
: php bin/magento setup:static-content:deploy -f
onepage.phtml
to my theme right?
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
requireJS
data-mage-init
and x-magento-init
but I haven't been able to implement it successfully for the checkout. I would appreciate you help;)
Mar 4, 2018 at 11:03