From f2301a467478c60d0496ac0cea2d99a09dde3e4c Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Tue, 13 Aug 2024 20:02:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20price=20calculation=20on?= =?UTF-8?q?=20pages=20with=20cart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This covers: Mini-Cart, Classic Cart, Block Cart, Classic Checkout, Block Checkout, Pay-Now page --- .../resources/js/GooglepayButton.js | 28 +++++++++-- .../resources/js/Helper/TransactionInfo.js | 47 +++++++++++++++---- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/modules/ppcp-googlepay/resources/js/GooglepayButton.js b/modules/ppcp-googlepay/resources/js/GooglepayButton.js index b0272cc9b..9f8524997 100644 --- a/modules/ppcp-googlepay/resources/js/GooglepayButton.js +++ b/modules/ppcp-googlepay/resources/js/GooglepayButton.js @@ -6,6 +6,11 @@ import { apmButtonsInit } from '../../../ppcp-button/resources/js/modules/Helper import TransactionInfo from './Helper/TransactionInfo'; class GooglepayButton { + /** + * @type {TransactionInfo} + */ + transactionInfo; + constructor( context, externalHandler, @@ -379,6 +384,16 @@ class GooglepayButton { ).update( paymentData ); const transactionInfo = this.transactionInfo; + // Check, if the current context uses the WC cart. + const hasRealCart = [ + 'checkout-block', + 'checkout', + 'cart-block', + 'cart', + 'mini-cart', + 'pay-now', + ].includes( this.context ); + this.log( 'onPaymentDataChanged:updatedData', updatedData ); this.log( 'onPaymentDataChanged:transactionInfo', @@ -405,10 +420,17 @@ class GooglepayButton { updatedData.shipping_options; } - transactionInfo.shippingFee = this.getShippingCosts( - paymentData?.shippingOptionData?.id, - updatedData.shipping_options + if ( updatedData.total && hasRealCart ) { + transactionInfo.setTotal( + updatedData.total, + updatedData.shipping_fee ); + } else { + transactionInfo.shippingFee = this.getShippingCosts( + paymentData?.shippingOptionData?.id, + updatedData.shipping_options + ); + } paymentDataRequestUpdate.newTransactionInfo = this.calculateNewTransactionInfo( transactionInfo ); diff --git a/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js b/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js index 012b024b5..93ffe2e6e 100644 --- a/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js +++ b/modules/ppcp-googlepay/resources/js/Helper/TransactionInfo.js @@ -15,21 +15,19 @@ export default class TransactionInfo { } set amount( newAmount ) { - this.#amount = Number( newAmount ) || 0; + this.#amount = this.toAmount( newAmount ); + } + + get amount() { + return this.#amount; } set shippingFee( newCost ) { - this.#shippingFee = Number( newCost ) || 0; + this.#shippingFee = this.toAmount( newCost ); } - set total( newTotal ) { - newTotal = Number( newTotal ) || 0; - - if ( ! newTotal ) { - return; - } - - this.#amount = newTotal - this.#shippingFee; + get shippingFee() { + return this.#shippingFee; } get currencyCode() { @@ -58,4 +56,33 @@ export default class TransactionInfo { totalPrice: this.totalPrice, }; } + + /** + * Converts the value to a number and rounds to a precision of 2 digits. + * + * @param {any} value - The value to sanitize. + * @return {number} Numeric value. + */ + toAmount( value ) { + value = Number( value ) || 0; + return Math.round( value * 100 ) / 100; + } + + setTotal( totalPrice, shippingFee ) { + totalPrice = this.toAmount( totalPrice ); + + if ( totalPrice ) { + this.shippingFee = shippingFee; + this.amount = totalPrice - this.shippingFee; + + console.log( + 'New Total Price:', + totalPrice, + '=', + this.amount, + '+', + this.shippingFee + ); + } + } }