I've been trying to find questions, articles, or documentation covering this, but haven't been able to find what I need or haven't understood it yet.
I have a custom module that a colleague wrote (who has since left) that adds some functionality via an API endpoint that we consume in a different application.
I now need to add some javascript to the /customer/account/ page.
I've read this answer and understand that I need to put something like the below into a config file:
<?xml version="1.0"?>
<layout version="0.1.0">
<customer_account_view>
<reference name="head">
<action method="addJs"><script>{my script}.js</script></action>
</reference>
</customer_account_view>
</layout>
Where does {my script}.js need to live inside my module directory, and which config file do I need to put the above block (if it is correct) inside my etc directory?
The current module directory looks like this:
UPDATE
The namespace for my module is James and the module is called AddToCart (i.e app/code/James/AddToCart.
Following the advice inside solution 2 by @xanka, I first created the customer_account_index.xml file at the following location app/code/James/AddToCart/view/frontend/layout/customer_account_index.xml with the following contents:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="james_addtocart::postMessage.js"/>
</head>
</page>
I then created the script inside the app/code/James/AddToCart/view/frontend/web/js/postMessage.js file:
console.log('test');
I then ran the following commands, unsure if necessary:
php bin/magento maintenance:enable
php bin/magento setup:upgrade
php -d memory_limit=2G bin/magento setup:di:compile
php -d memory_limit=8G -d max_execution_time=18000 bin/magento setup:static-content:deploy -f
php bin/magento maintenance:disable
php bin/magento cache:clean
php bin/magento cache:flush
redis-cli -p 6379 flushall
Everything ran successfully, however I cannot see 'test' in my dev console when I log in and visit /customer/account and when I view source, I cannot see that my script is included in the head. Where have I gone wrong?
-
The reference you are following is for Magento 1. With Magento 2 the approach is different. Please refer : devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/… and magento.stackexchange.com/a/156144/2443Jaimin Sutariya– Jaimin Sutariya2020年02月25日 06:48:53 +00:00Commented Feb 25, 2020 at 6:48
1 Answer 1
Hope you doing well!
To way right there.
1: in your theme create new layout file in
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="Magento_Customer::your_custom.js"/>
</head>
</page>
app/code/design/frontend/{theme_theme}/Customer/layout/customer_account_index.xml.
push your js file in your current theme like:
app/code/design/frontend/{theme_theme}/Customer/web/js/your_custom.js
2: You can create new module and push you code like
app/code/namespace/module_name/view/frontend/layout/customer_account_index.xml.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="namespace_modulename::your_custom.js"/>
</head>
</page>
Then create new {your_custom.is} in
app/code/namespace/module_name/view/frontend/web/js/{your_custom.js}
-
My current extension is not a theme. Can I do it from the existing extension inside
app/codeor do I have to create a theme to do this?James– James2020年02月25日 07:45:44 +00:00Commented Feb 25, 2020 at 7:45 -
Sorry I see you mention how to do it from my existing extension on option 2. I will try that!James– James2020年02月25日 07:46:42 +00:00Commented Feb 25, 2020 at 7:46
-
I tried option 2, and whilst I get no errors, I don't see my script running. See my updated question for steps followed. I'd really appreciate it if you could see where I have gone wrong.James– James2020年02月25日 10:34:48 +00:00Commented Feb 25, 2020 at 10:34
-
I have posted a new question to address the problems I am having with this not working; magento.stackexchange.com/questions/305336/…James– James2020年02月25日 21:03:10 +00:00Commented Feb 25, 2020 at 21:03
-
Hi @James, I answer the question pls checkxanka– xanka2020年02月26日 02:07:55 +00:00Commented Feb 26, 2020 at 2:07
Explore related questions
See similar questions with these tags.