Prevent displaying smart button multiple times on variable product page

This commit is contained in:
George Burduli 2024-07-15 16:45:10 +04:00
parent 36a13f6500
commit 397acdd2bf
No known key found for this signature in database
GPG key ID: 572A97DFDA3D2E5C

View file

@ -7,6 +7,7 @@ import {getPlanIdFromVariation} from "../Helper/Subscriptions"
import SimulateCart from "../Helper/SimulateCart"; import SimulateCart from "../Helper/SimulateCart";
import {strRemoveWord, strAddWord, throttle} from "../Helper/Utils"; import {strRemoveWord, strAddWord, throttle} from "../Helper/Utils";
import merge from "deepmerge"; import merge from "deepmerge";
import {debounce} from "../../../../../ppcp-blocks/resources/js/Helper/debounce";
class SingleProductBootstap { class SingleProductBootstap {
constructor(gateway, renderer, errorHandler) { constructor(gateway, renderer, errorHandler) {
@ -17,7 +18,8 @@ class SingleProductBootstap {
this.formSelector = 'form.cart'; this.formSelector = 'form.cart';
// Prevent simulate cart being called too many times in a burst. // Prevent simulate cart being called too many times in a burst.
this.simulateCartThrottled = throttle(this.simulateCart, this.gateway.simulate_cart.throttling || 5000); this.simulateCartThrottled = throttle(this.simulateCart.bind(this), this.gateway.simulate_cart.throttling || 5000);
this.debouncedHandleChange = debounce(this.handleChange.bind(this), 100);
this.renderer.onButtonsInit(this.gateway.button.wrapper, () => { this.renderer.onButtonsInit(this.gateway.button.wrapper, () => {
this.handleChange(); this.handleChange();
@ -31,7 +33,7 @@ class SingleProductBootstap {
} }
handleChange() { handleChange() {
this.subscriptionButtonsLoaded = false this.subscriptionButtonsLoaded = false;
if (!this.shouldRender()) { if (!this.shouldRender()) {
this.renderer.disableSmartButtons(this.gateway.button.wrapper); this.renderer.disableSmartButtons(this.gateway.button.wrapper);
@ -65,8 +67,9 @@ class SingleProductBootstap {
} }
jQuery(document).on('change', this.formSelector, () => { jQuery(document).on('change', this.formSelector, () => {
this.handleChange(); this.debouncedHandleChange();
}); });
this.mutationObserver.observe(form, { childList: true, subtree: true }); this.mutationObserver.observe(form, { childList: true, subtree: true });
const addToCartButton = form.querySelector('.single_add_to_cart_button'); const addToCartButton = form.querySelector('.single_add_to_cart_button');