yireo / Yireo_GoogleTagManager2

Open Software License 3.0
147 stars 76 forks source link

add_to_cart event is fired twice in Hyva based theme #203

Closed machraf closed 3 months ago

machraf commented 11 months ago

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 commented 11 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

@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 commented 6 months ago

@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);
}
jissereitsma commented 3 months ago

This change is now included in 3.9.3.

machraf commented 3 months ago

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.

jissereitsma commented 3 months ago

Good to hear, thanks!