From 7db17b6426d2ba111da540cf5629c9927b1bed3f Mon Sep 17 00:00:00 2001 From: David Remer Date: Wed, 19 Aug 2020 13:40:29 +0300 Subject: [PATCH] rerender when price on single product changes --- .../ppcp-button/resources/js/button.js | 1 + .../ContextBootstrap/SingleProductBootstap.js | 12 +++++++- .../js/modules/Renderer/MessageRenderer.js | 30 ++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/modules.local/ppcp-button/resources/js/button.js b/modules.local/ppcp-button/resources/js/button.js index 437b1e412..f52b8127d 100644 --- a/modules.local/ppcp-button/resources/js/button.js +++ b/modules.local/ppcp-button/resources/js/button.js @@ -27,6 +27,7 @@ const bootstrap = () => { const singleProductBootstrap = new SingleProductBootstap( PayPalCommerceGateway, renderer, + messageRenderer, ); singleProductBootstrap.init(); diff --git a/modules.local/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules.local/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index f9d02449b..fc9bc6ca6 100644 --- a/modules.local/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules.local/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -3,9 +3,10 @@ import UpdateCart from "../Helper/UpdateCart"; import SingleProductActionHandler from "../ActionHandler/SingleProductActionHandler"; class SingleProductBootstap { - constructor(gateway, renderer) { + constructor(gateway, renderer, messages) { this.gateway = gateway; this.renderer = renderer; + this.messages = messages; } init() { @@ -35,6 +36,15 @@ class SingleProductBootstap { () => { this.renderer.showButtons(this.gateway.button.wrapper); this.renderer.showButtons(this.gateway.hosted_fields.wrapper); + let priceText = "0"; + if (document.querySelector('form.cart ins .woocommerce-Price-amount')) { + priceText = document.querySelector('form.cart ins .woocommerce-Price-amount').innerText; + } + else if (document.querySelector('form.cart .woocommerce-Price-amount')) { + priceText = document.querySelector('form.cart .woocommerce-Price-amount').innerText; + } + const amount = parseInt(priceText.replace(/([^\d,\.\s]*)/g, '')); + this.messages.renderWithAmount(amount) }, () => { this.renderer.hideButtons(this.gateway.button.wrapper); diff --git a/modules.local/ppcp-button/resources/js/modules/Renderer/MessageRenderer.js b/modules.local/ppcp-button/resources/js/modules/Renderer/MessageRenderer.js index 2afbddaeb..a34fd2489 100644 --- a/modules.local/ppcp-button/resources/js/modules/Renderer/MessageRenderer.js +++ b/modules.local/ppcp-button/resources/js/modules/Renderer/MessageRenderer.js @@ -5,10 +5,7 @@ class MessageRenderer { } render() { - if (typeof paypal.Messages === 'undefined' || typeof this.config.wrapper === 'undefined' ) { - return; - } - if (! document.querySelector(this.config.wrapper)) { + if (! this.shouldRender()) { return; } @@ -18,5 +15,30 @@ class MessageRenderer { style: this.config.style }).render(this.config.wrapper); } + + renderWithAmount(amount) { + + if (! this.shouldRender()) { + return; + } + + console.log(amount); + paypal.Messages({ + amount, + placement: this.config.placement, + style: this.config.style + }).render(this.config.wrapper); + } + + shouldRender() { + + if (typeof paypal.Messages === 'undefined' || typeof this.config.wrapper === 'undefined' ) { + return false; + } + if (! document.querySelector(this.config.wrapper)) { + return false; + } + return true; + } } export default MessageRenderer; \ No newline at end of file