Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_to_cart event is fired twice in Hyva based theme #203

Open
machraf opened this issue Dec 14, 2023 · 5 comments
Open

add_to_cart event is fired twice in Hyva based theme #203

machraf opened this issue Dec 14, 2023 · 5 comments

Comments

@machraf
Copy link

machraf commented Dec 14, 2023

I'm noticing that the add_to_cart event is fired twice in a Hyva-based theme, the first time when a user clicks on the add-to-cart button and a second time after a page reloads. it seems like the event is not removed properly from mage-cache-storage in view/frontend/templates/hyva/script-additions.phtml line 58-63

I can provide more details if needed.

@jissereitsma
Copy link
Collaborator

Thanks for posting. I have tested this in 3 environments of my own. And in my cases, the add_to_cart event only triggers once: This makes sense, because the only way that the module out-of-the-box triggers the event is via a PHP-based event, so the customerStorage mechanism.

What did you do to also add the event via a mouse-click on the button? There must be some custom code involved, because it is not part of this module.

@thehorse2000
Copy link

Hey, Any updates on this?
I am also facing the same issue where the add_to_cart event is being triggered two times, one after pressing the add-to-cart button and another time when refreshing or navigating to any page.

@jissereitsma
Copy link
Collaborator

Unfortunately, I'm unable to reproduce this issue. In my case, with Hyva, the add-to-cart button submits a form to a controller, which then puts the event add_to_cart in the internal queue. The page reloads and then the event is picked up again.

Note that there is no functionality in this module to generate an add_to_cart event based on a mouse-click. If you find the custom code that does so, most likely you have the answer to this problem.

@MaximGns
Copy link

MaximGns commented May 13, 2024

@machraf do you use the ajax add-to-cart functionality?

We encounter the same issue, and i guess the issues lies around following lines in script-additions.phtml.

--> let mageCacheStorage = window.localStorage.getItem('mage-cache-storage');

If you add a product to your cart and refreshes the private content, it seems to not update the local storage.
The updated private content is sent through the event though.

If I replace following

let mageCacheStorage = window.localStorage.getItem('mage-cache-storage');

with

let mageCacheStorage = event.detail.data;

It always uses the latest private content and seems to not trigger the add_to_cart event twice when reloading/navigating away after an add to cart.

@thehorse2000 and @machraf could you test this change as well? If this is a working bugfix, I will create a PR.

Hyva themes version: 1.1.23
Yireo GTM version: 3.5.8

@jansentjeu
Copy link

jansentjeu commented May 23, 2024

@MaximGns We also use the ajax add-to cart functionality and are encountering the same issue. After applying your fix it indeed seems to work!
The code below must of course also be removed when you use let mageCacheStorage = event.detail.data;

if (mageCacheStorage) {
 mageCacheStorage = JSON.parse(mageCacheStorage);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants