mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +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';
|
import TransactionInfo from './Helper/TransactionInfo';
|
||||||
|
|
||||||
class GooglepayButton {
|
class GooglepayButton {
|
||||||
|
/**
|
||||||
|
* @type {TransactionInfo}
|
||||||
|
*/
|
||||||
|
transactionInfo;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
context,
|
context,
|
||||||
externalHandler,
|
externalHandler,
|
||||||
|
@ -379,6 +384,16 @@ class GooglepayButton {
|
||||||
).update( paymentData );
|
).update( paymentData );
|
||||||
const transactionInfo = this.transactionInfo;
|
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:updatedData', updatedData );
|
||||||
this.log(
|
this.log(
|
||||||
'onPaymentDataChanged:transactionInfo',
|
'onPaymentDataChanged:transactionInfo',
|
||||||
|
@ -405,10 +420,17 @@ class GooglepayButton {
|
||||||
updatedData.shipping_options;
|
updatedData.shipping_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
transactionInfo.shippingFee = this.getShippingCosts(
|
if ( updatedData.total && hasRealCart ) {
|
||||||
paymentData?.shippingOptionData?.id,
|
transactionInfo.setTotal(
|
||||||
updatedData.shipping_options
|
updatedData.total,
|
||||||
|
updatedData.shipping_fee
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
transactionInfo.shippingFee = this.getShippingCosts(
|
||||||
|
paymentData?.shippingOptionData?.id,
|
||||||
|
updatedData.shipping_options
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
paymentDataRequestUpdate.newTransactionInfo =
|
paymentDataRequestUpdate.newTransactionInfo =
|
||||||
this.calculateNewTransactionInfo( transactionInfo );
|
this.calculateNewTransactionInfo( transactionInfo );
|
||||||
|
|
|
@ -15,21 +15,19 @@ export default class TransactionInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
set amount( newAmount ) {
|
set amount( newAmount ) {
|
||||||
this.#amount = Number( newAmount ) || 0;
|
this.#amount = this.toAmount( newAmount );
|
||||||
|
}
|
||||||
|
|
||||||
|
get amount() {
|
||||||
|
return this.#amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
set shippingFee( newCost ) {
|
set shippingFee( newCost ) {
|
||||||
this.#shippingFee = Number( newCost ) || 0;
|
this.#shippingFee = this.toAmount( newCost );
|
||||||
}
|
}
|
||||||
|
|
||||||
set total( newTotal ) {
|
get shippingFee() {
|
||||||
newTotal = Number( newTotal ) || 0;
|
return this.#shippingFee;
|
||||||
|
|
||||||
if ( ! newTotal ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#amount = newTotal - this.#shippingFee;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get currencyCode() {
|
get currencyCode() {
|
||||||
|
@ -58,4 +56,33 @@ export default class TransactionInfo {
|
||||||
totalPrice: this.totalPrice,
|
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