Update Pay Later amount on the cart page when cart total changes

This commit is contained in:
Alex P 2023-06-16 11:39:20 +03:00
parent 74611528b5
commit b6a85f0d13
No known key found for this signature in database
GPG key ID: 54487A734A204D71
3 changed files with 18 additions and 3 deletions

View file

@ -154,6 +154,7 @@ const bootstrap = () => {
const cartBootstrap = new CartBootstrap(
PayPalCommerceGateway,
renderer,
messageRenderer,
errorHandler,
);

View file

@ -2,10 +2,12 @@ import CartActionHandler from '../ActionHandler/CartActionHandler';
import {setVisible} from "../Helper/Hiding";
class CartBootstrap {
constructor(gateway, renderer, errorHandler) {
constructor(gateway, renderer, messages, errorHandler) {
this.gateway = gateway;
this.renderer = renderer;
this.messages = messages;
this.errorHandler = errorHandler;
this.lastAmount = this.gateway.messages.amount;
}
init() {
@ -31,11 +33,16 @@ class CartBootstrap {
return;
}
const newParams = result.data;
const newParams = result.data.url_params;
const reloadRequired = this.gateway.url_params.intent !== newParams.intent;
// TODO: should reload the script instead
setVisible(this.gateway.button.wrapper, !reloadRequired)
if (this.lastAmount !== result.data.amount) {
this.lastAmount = result.data.amount;
this.messages.renderWithAmount(this.lastAmount);
}
});
});
}
@ -61,6 +68,8 @@ class CartBootstrap {
this.renderer.render(
actionHandler.configuration()
);
this.messages.renderWithAmount(this.lastAmount);
}
}

View file

@ -67,7 +67,12 @@ class CartScriptParamsEndpoint implements EndpointInterface {
try {
$script_data = $this->smart_button->script_data();
wp_send_json_success( $script_data['url_params'] );
wp_send_json_success(
array(
'url_params' => $script_data['url_params'],
'amount' => WC()->cart->get_total( 'raw' ),
)
);
return true;
} catch ( Throwable $error ) {