Yireo_GoogleTagManager2 icon indicating copy to clipboard operation
Yireo_GoogleTagManager2 copied to clipboard

add_to_cart event is fired twice in Hyva based theme

Open machraf opened this issue 1 year ago • 5 comments

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.

machraf avatar Dec 14 '23 09:12 machraf

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.

jissereitsma avatar Dec 14 '23 11:12 jissereitsma

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.

thehorse2000 avatar Apr 28 '24 19:04 thehorse2000

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.

jissereitsma avatar Apr 29 '24 11:04 jissereitsma

@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

MaximGns avatar May 13 '24 11:05 MaximGns

@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);
}

jansentjeu avatar May 23 '24 09:05 jansentjeu

This change is now included in 3.9.3.

jissereitsma avatar Aug 02 '24 12:08 jissereitsma

Thanks all for your contributions, and sorry for not being able to test because I no longer work on the project where I faced this. So if it's confirmed that this is fixed please feel free to close the issue. thanks.

machraf avatar Aug 05 '24 11:08 machraf

Good to hear, thanks!

jissereitsma avatar Aug 05 '24 13:08 jissereitsma