3

I'm working on a custom module for GTM.

The last part I facing issue is with require js.

I want to trigger a custom JS on "add to cart" click.

I made a requirejs-config.js file in Vendorname/modulename/view/frontend with this :

alert("Hello");
var config = {
    map: {
        '*': {
            "catalogAddToCart":    'Vendorname_modulename/js/customCatalogAddToCart'
        }
    }
};

If I clear cache deploy static content (with -f mode as I'm in developer mode) I got my alert.

But in my Vendorname/modulename/view/frontend/web/js/customCatalogAddToCart.js I add

alert("world");

In the network panel, I didn't see the file customCatalogAddToCart.js and also didn't get the second Alert.

Do you know what's wrong?

  • please remove alert from require js. – kunj Jun 7 '18 at 10:56
1

Please changes

"catalogAddToCart":
'Vendorname_modulename/js/customCatalogAddToCart'

to

'Magento_Catalog/js/catalog-add-to-cart':'Vendorname_modulename/js/customCatalogAddToCart'

try again.

  • Thanks It's better like that my script is loaded ! now I have to get all value needed (sku, productname,price) and i'm good. Many thanks ! – Mitchum Jun 7 '18 at 11:23
1

Here is my situation:

I have following module:

app/code/Mymodule/Test/view/frontend/requirejs-config.js

Here is the content of this file:

var config = {
    map: {
        '*': {
            catalogAddToCart: "Mymodule_Test/js/customCatalogAddToCart"
        }
    }
};

My Javascripts are at following location:

app/code/Mymodule/Test/view/frontend/web/js/customCatalogAddToCart.js

In the PHTML file:

app/code/Mymodule/Test/view/frontend/templates/home.phtml

I added the lines:

require(['jquery', 'catalogAddToCart'],function($){
    alert("world");
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.