mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-31 06:52:50 +08:00
🐛 Fix price calculation on pages with cart
This covers: Mini-Cart, Classic Cart, Block Cart, Classic Checkout, Block Checkout, Pay-Now page
This commit is contained in:
parent
8f191c2d49
commit
f2301a4674
2 changed files with 62 additions and 13 deletions
|
@ -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 );
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue