diff --git a/assets/js/unit-price-observer-queue.js b/assets/js/unit-price-observer-queue.js index aa354476e..32404dbdb 100644 --- a/assets/js/unit-price-observer-queue.js +++ b/assets/js/unit-price-observer-queue.js @@ -50,7 +50,8 @@ window.germanized = window.germanized || {}; observer = current.observer, priceData = current.priceData, priceSelector = current.priceSelector, - isPrimary = current.isPrimary; + isPrimary = current.isPrimary, + unitPrices = self.getUnitPricesFromMap( priceData.unit_price ); if ( observer ) { if ( data.products.hasOwnProperty( queueId ) ) { @@ -64,33 +65,22 @@ window.germanized = window.germanized || {}; */ if ( parseInt( response.product_id ) === observer.getCurrentProductId( observer ) ) { if ( response.hasOwnProperty( 'unit_price_html' ) ) { - observer.unsetUnitPriceLoading( observer, priceData.unit_price, response.unit_price_html ); + observer.unsetUnitPriceLoading( observer, unitPrices, response.unit_price_html ); } else { - observer.unsetUnitPriceLoading( observer, priceData.unit_price ); + observer.unsetUnitPriceLoading( observer, unitPrices ); } } else { - observer.unsetUnitPriceLoading( observer, priceData.unit_price ); + observer.unsetUnitPriceLoading( observer, unitPrices ); } observer.startObserver( observer, priceSelector, isPrimary ); } else { observer.stopObserver( observer, priceSelector ); - observer.unsetUnitPriceLoading( observer, priceData.unit_price ); + observer.unsetUnitPriceLoading( observer, unitPrices ); observer.startObserver( observer, priceSelector, isPrimary ); } } }); - - Object.keys( data.products ).forEach( function( responseProductId ) { - if ( currentQueue.hasOwnProperty( responseProductId ) ) { - var current = currentQueue[ responseProductId ], - $unitPrice = $( current.priceData.unit_price ); - - if ( $unitPrice.data( 'unitPriceObserver' ) ) { - $unitPrice.data( 'unitPriceObserver' ) - } - } - }); }, error: function() { Object.keys( currentQueue ).forEach( function( queueId ) { @@ -98,11 +88,12 @@ window.germanized = window.germanized || {}; observer = current.observer, priceData = current.priceData, priceSelector = current.priceSelector, - isPrimary = current.isPrimary; + isPrimary = current.isPrimary, + unitPrices = self.getUnitPricesFromMap( priceData.unit_price ); if ( observer ) { observer.stopObserver( observer, priceSelector ); - observer.unsetUnitPriceLoading( observer, priceData.unit_price ); + observer.unsetUnitPriceLoading( observer, unitPrices ); observer.startObserver( observer, priceSelector, isPrimary ); } }); @@ -111,21 +102,47 @@ window.germanized = window.germanized || {}; } ); }, - getQueueKey: function( productId, selector ) { - return ( productId + selector ).replace( /[^a-zA-Z0-9]/g, '' ); + getUnitPricesFromMap: function( unitPriceMap ) { + let unitPrices = []; + + unitPriceMap.forEach( function( unitPrice ) { + unitPrices = $.merge( unitPrices, $( unitPrice ) ); + } ); + + return $( unitPrices ); + }, + + getQueueKey: function( productId ) { + return ( productId + '' ).replace( /[^a-zA-Z0-9]/g, '' ); }, add: function( observer, productId, priceData, priceSelector, isPrimary ) { var self = germanized.unit_price_observer_queue, - queueKey = self.getQueueKey( productId, priceSelector ); - - self.queue[ queueKey ] = { - 'productId' : productId, - 'observer' : observer, - 'priceData' : priceData, - 'priceSelector': priceSelector, - 'isPrimary' : isPrimary - }; + queueKey = self.getQueueKey( productId ); + + if ( self.queue.hasOwnProperty( queueKey ) ) { + priceData['unit_price'].each( function( i, obj ) { + if ( ! self.queue[ queueKey ]['priceData']['unit_price'].has( obj ) ) { + self.queue[ queueKey ]['priceData']['unit_price'].set( obj, obj ); + } + }); + } else { + var unitPrices = new Map(); + + priceData['unit_price'].each( function( i, obj ) { + unitPrices.set( obj, obj ); + }); + + priceData['unit_price'] = unitPrices; + + self.queue[ queueKey ] = { + 'productId' : productId, + 'observer' : observer, + 'priceData' : priceData, + 'priceSelector': priceSelector, + 'isPrimary' : isPrimary + }; + } clearTimeout( self.timeout ); self.timeout = setTimeout( self.execute, 500 ); diff --git a/assets/js/unit-price-observer-queue.min.js b/assets/js/unit-price-observer-queue.min.js index a0fcd995d..d0a9c687b 100644 --- a/assets/js/unit-price-observer-queue.min.js +++ b/assets/js/unit-price-observer-queue.min.js @@ -1 +1 @@ -window.germanized=window.germanized||{},function(t,a){a.unit_price_observer_queue={queue:{},timeout:null,params:{},request:null,init:function(){this.params=wc_gzd_unit_price_observer_queue_params,this.queue={},this.timeout=null,this.request=null},execute:function(){var e=a.unit_price_observer_queue,r=[],c={...e.queue};e.queue={},e.timeout=null,Object.keys(c).forEach(function(e){r=r.concat([{product_id:c[e].productId,price:c[e].priceData.price,price_sale:c[e].priceData.sale_price,quantity:c[e].priceData.quantity,key:e}])}),e.request=t.ajax({type:"POST",url:e.params.wc_ajax_url.toString().replace("%%endpoint%%","gzd_refresh_unit_price"),data:{security:e.params.refresh_unit_price_nonce,products:r},success:function(n){Object.keys(c).forEach(function(e){var r=c[e],t=r.observer,i=r.priceData,u=r.priceSelector,r=r.isPrimary;t&&(n.products.hasOwnProperty(e)?(e=n.products[e],t.stopObserver(t,u),parseInt(e.product_id)===t.getCurrentProductId(t)&&e.hasOwnProperty("unit_price_html")?t.unsetUnitPriceLoading(t,i.unit_price,e.unit_price_html):t.unsetUnitPriceLoading(t,i.unit_price)):(t.stopObserver(t,u),t.unsetUnitPriceLoading(t,i.unit_price)),t.startObserver(t,u,r))}),Object.keys(n.products).forEach(function(e){c.hasOwnProperty(e)&&(e=c[e],(e=t(e.priceData.unit_price)).data("unitPriceObserver")&&e.data("unitPriceObserver"))})},error:function(){Object.keys(c).forEach(function(e){var r=c[e],t=r.observer,i=r.priceData,e=r.priceSelector,r=r.isPrimary;t&&(t.stopObserver(t,e),t.unsetUnitPriceLoading(t,i.unit_price),t.startObserver(t,e,r))})},dataType:"json"})},getQueueKey:function(e,r){return(e+r).replace(/[^a-zA-Z0-9]/g,"")},add:function(e,r,t,i,u){var n=a.unit_price_observer_queue,c=n.getQueueKey(r,i);n.queue[c]={productId:r,observer:e,priceData:t,priceSelector:i,isPrimary:u},clearTimeout(n.timeout),n.timeout=setTimeout(n.execute,500)}},t(document).ready(function(){a.unit_price_observer_queue.init()})}(jQuery,window.germanized); \ No newline at end of file +window.germanized=window.germanized||{},function(t,o){o.unit_price_observer_queue={queue:{},timeout:null,params:{},request:null,init:function(){this.params=wc_gzd_unit_price_observer_queue_params,this.queue={},this.timeout=null,this.request=null},execute:function(){var c=o.unit_price_observer_queue,r=[],a={...c.queue};c.queue={},c.timeout=null,Object.keys(a).forEach(function(e){r=r.concat([{product_id:a[e].productId,price:a[e].priceData.price,price_sale:a[e].priceData.sale_price,quantity:a[e].priceData.quantity,key:e}])}),c.request=t.ajax({type:"POST",url:c.params.wc_ajax_url.toString().replace("%%endpoint%%","gzd_refresh_unit_price"),data:{security:c.params.refresh_unit_price_nonce,products:r},success:function(n){Object.keys(a).forEach(function(e){var r=a[e],t=r.observer,i=r.priceData,u=r.priceSelector,r=r.isPrimary,i=c.getUnitPricesFromMap(i.unit_price);t&&(n.products.hasOwnProperty(e)?(e=n.products[e],t.stopObserver(t,u),parseInt(e.product_id)===t.getCurrentProductId(t)&&e.hasOwnProperty("unit_price_html")?t.unsetUnitPriceLoading(t,i,e.unit_price_html):t.unsetUnitPriceLoading(t,i)):(t.stopObserver(t,u),t.unsetUnitPriceLoading(t,i)),t.startObserver(t,u,r))})},error:function(){Object.keys(a).forEach(function(e){var r=a[e],t=r.observer,i=r.priceData,e=r.priceSelector,r=r.isPrimary,i=c.getUnitPricesFromMap(i.unit_price);t&&(t.stopObserver(t,e),t.unsetUnitPriceLoading(t,i),t.startObserver(t,e,r))})},dataType:"json"})},getUnitPricesFromMap:function(e){let r=[];return e.forEach(function(e){r=t.merge(r,t(e))}),t(r)},getQueueKey:function(e){return(e+"").replace(/[^a-zA-Z0-9]/g,"")},add:function(e,r,t,i,u){var n,c=o.unit_price_observer_queue,a=c.getQueueKey(r);c.queue.hasOwnProperty(a)?t.unit_price.each(function(e,r){c.queue[a].priceData.unit_price.has(r)||c.queue[a].priceData.unit_price.set(r,r)}):(n=new Map,t.unit_price.each(function(e,r){n.set(r,r)}),t.unit_price=n,c.queue[a]={productId:r,observer:e,priceData:t,priceSelector:i,isPrimary:u}),clearTimeout(c.timeout),c.timeout=setTimeout(c.execute,500)}},t(document).ready(function(){o.unit_price_observer_queue.init()})}(jQuery,window.germanized); \ No newline at end of file diff --git a/assets/js/unit-price-observer.js b/assets/js/unit-price-observer.js index 96f00f84b..baecea47f 100644 --- a/assets/js/unit-price-observer.js +++ b/assets/js/unit-price-observer.js @@ -444,6 +444,10 @@ $( function() { if ( typeof wc_gzd_unit_price_observer_params !== 'undefined' ) { $( wc_gzd_unit_price_observer_params.wrapper ).each( function() { + if ( $( this ).is( 'body' ) ) { + return; + } + $( this ).wc_germanized_unit_price_observer(); }); } diff --git a/assets/js/unit-price-observer.min.js b/assets/js/unit-price-observer.min.js index 0509e83ea..548a6d0bc 100644 --- a/assets/js/unit-price-observer.min.js +++ b/assets/js/unit-price-observer.min.js @@ -1 +1 @@ -!function(c,t){function r(r){var e,a=this;if(a.params=wc_gzd_unit_price_observer_params,a.$wrapper=r.closest(a.params.wrapper),a.$form=0"+e+"");var t=r.find("span:first").width();return r.html(e),t},r.prototype.getPriceNode=function(r,e,t){t=void 0!==t&&t;e=r.$wrapper.find(e+":not(.price-unit):visible").not(".variations_form .single_variation .price").first();return t&&r.isVar&&(e.length<=0||!r.replacePrice)?e=r.$wrapper.find(".woocommerce-variation-price span.price:not(.price-unit):visible:last"):t&&e.length<=0&&(e=r.$wrapper.find(".price:not(.price-unit):visible:last")),e},r.prototype.getObserverNode=function(r,e,t){e=r.getPriceNode(r,e,t);return e=t&&r.isVar&&!r.replacePrice?r.$wrapper.find(".single_variation:last"):e},r.prototype.getUnitPriceNode=function(r,e){return e.length<=0?[]:"SPAN"===e[0].tagName?r.$wrapper.find(".price-unit"):r.$wrapper.find(".price-unit:not(.wc-gzd-additional-info-placeholder, .wc-gzd-additional-info-loop)")},r.prototype.stopObserver=function(r,e){e=r.getObserver(r,e);e&&e.disconnect()},r.prototype.startObserver=function(r,e,t){var i=r.getObserver(r,e),t=r.getObserverNode(r,e,t);return!!i&&(r.stopObserver(r,e),0'),e.addClass("wc-gzd-loading"),e.data("org-html",i)),i},r.prototype.unsetUnitPriceLoading=function(r,e,t){t=t||e.data("org-html"),e.html(t),e.hasClass("wc-gzd-loading")&&e.removeClass("wc-gzd-loading").show()},r.prototype.refreshUnitPrice=function(r,e,t,i){germanized.unit_price_observer_queue.add(r,r.getCurrentProductId(r),e,t,i)},c.fn.wc_germanized_unit_price_observer=function(){return c(this).data("unitPriceObserver")&&c(this).data("unitPriceObserver").destroy(),new r(this),this},c(function(){"undefined"!=typeof wc_gzd_unit_price_observer_params&&c(wc_gzd_unit_price_observer_params.wrapper).each(function(){c(this).wc_germanized_unit_price_observer()})})}(jQuery,window,document),window.germanized=window.germanized||{}; \ No newline at end of file +!function(c,t){function r(r){var e,a=this;if(a.params=wc_gzd_unit_price_observer_params,a.$wrapper=r.closest(a.params.wrapper),a.$form=0"+e+"");var t=r.find("span:first").width();return r.html(e),t},r.prototype.getPriceNode=function(r,e,t){t=void 0!==t&&t;e=r.$wrapper.find(e+":not(.price-unit):visible").not(".variations_form .single_variation .price").first();return t&&r.isVar&&(e.length<=0||!r.replacePrice)?e=r.$wrapper.find(".woocommerce-variation-price span.price:not(.price-unit):visible:last"):t&&e.length<=0&&(e=r.$wrapper.find(".price:not(.price-unit):visible:last")),e},r.prototype.getObserverNode=function(r,e,t){e=r.getPriceNode(r,e,t);return e=t&&r.isVar&&!r.replacePrice?r.$wrapper.find(".single_variation:last"):e},r.prototype.getUnitPriceNode=function(r,e){return e.length<=0?[]:"SPAN"===e[0].tagName?r.$wrapper.find(".price-unit"):r.$wrapper.find(".price-unit:not(.wc-gzd-additional-info-placeholder, .wc-gzd-additional-info-loop)")},r.prototype.stopObserver=function(r,e){e=r.getObserver(r,e);e&&e.disconnect()},r.prototype.startObserver=function(r,e,t){var i=r.getObserver(r,e),t=r.getObserverNode(r,e,t);return!!i&&(r.stopObserver(r,e),0'),e.addClass("wc-gzd-loading"),e.data("org-html",i)),i},r.prototype.unsetUnitPriceLoading=function(r,e,t){t=t||e.data("org-html"),e.html(t),e.hasClass("wc-gzd-loading")&&e.removeClass("wc-gzd-loading").show()},r.prototype.refreshUnitPrice=function(r,e,t,i){germanized.unit_price_observer_queue.add(r,r.getCurrentProductId(r),e,t,i)},c.fn.wc_germanized_unit_price_observer=function(){return c(this).data("unitPriceObserver")&&c(this).data("unitPriceObserver").destroy(),new r(this),this},c(function(){"undefined"!=typeof wc_gzd_unit_price_observer_params&&c(wc_gzd_unit_price_observer_params.wrapper).each(function(){c(this).is("body")||c(this).wc_germanized_unit_price_observer()})})}(jQuery,window,document),window.germanized=window.germanized||{}; \ No newline at end of file diff --git a/includes/class-wc-gzd-checkout.php b/includes/class-wc-gzd-checkout.php index a7c7ea29d..1ebb62ddc 100644 --- a/includes/class-wc-gzd-checkout.php +++ b/includes/class-wc-gzd-checkout.php @@ -281,7 +281,7 @@ public function maybe_adjust_photovoltaic_cart_data( $cart ) { if ( $checkbox = wc_gzd_get_legal_checkbox( 'photovoltaic_systems' ) ) { if ( $checkbox->is_enabled() ) { - if ( $this->checkbox_is_checked( $checkbox ) && wc_gzd_cart_applies_for_photovoltaic_system_vat_exemption() ) { + if ( $this->checkbox_is_checked( $checkbox ) && wc_gzd_cart_applies_for_photovoltaic_system_vat_exemption( $cart->get_cart() ) ) { foreach ( $cart->get_cart() as $cart_item_key => $values ) { $_product = apply_filters( 'woocommerce_cart_item_product', $values['data'], $values, $cart_item_key );