Render pay later blocks

This commit is contained in:
Alex P 2023-12-06 17:31:56 +02:00
parent d8ff7f79e6
commit baa3afeb9b
No known key found for this signature in database
GPG key ID: 54487A734A204D71
6 changed files with 46 additions and 20 deletions

View file

@ -1,13 +1,18 @@
import {setVisible} from "../Helper/Hiding";
import MessageRenderer from "../Renderer/MessageRenderer";
class MessagesBootstrap {
constructor(gateway, messageRenderer) {
this.gateway = gateway;
this.renderer = messageRenderer;
this.renderers = [messageRenderer];
this.lastAmount = this.gateway.messages.amount;
}
init() {
Array.from(document.querySelectorAll('.ppcp-paylater-message-block')).forEach(blockElement => {
this.renderers.push(new MessageRenderer({wrapper: '#' + blockElement.id}));
});
jQuery(document.body).on('ppcp_cart_rendered ppcp_checkout_rendered', () => {
this.render();
});
@ -27,28 +32,30 @@ class MessagesBootstrap {
this.render();
}
shouldShow() {
shouldShow(renderer) {
if (this.gateway.messages.is_hidden === true) {
return false;
}
const eventData = {result: true}
jQuery(document.body).trigger('ppcp_should_show_messages', [eventData]);
jQuery(document.body).trigger('ppcp_should_show_messages', [eventData, renderer.config.wrapper]);
return eventData.result;
}
shouldRender() {
return this.shouldShow() && this.renderer.shouldRender();
}
render() {
setVisible(this.gateway.messages.wrapper, this.shouldShow());
this.renderers.forEach(renderer => {
const shouldShow = this.shouldShow(renderer);
setVisible(renderer.config.wrapper, shouldShow);
if (!shouldShow) {
return;
}
if (!this.shouldRender()) {
return;
}
if (!renderer.shouldRender()) {
return;
}
this.renderer.renderWithAmount(this.lastAmount);
renderer.renderWithAmount(this.lastAmount);
});
}
}

View file

@ -15,9 +15,13 @@ class MessageRenderer {
const options = {
amount,
placement: this.config.placement,
style: this.config.style
};
if (this.config.placement) {
options.placement = this.config.placement;
}
if (this.config.style) {
options.style = this.config.style;
}
// sometimes the element is destroyed while the options stay the same
if (document.querySelector(this.config.wrapper).getAttribute('data-render-number') !== this.currentNumber.toString()) {

View file

@ -620,6 +620,10 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
return false;
}
if ( has_block( 'woocommerce-paypal-payments/paylater-messages' ) ) {
return true;
}
$location = $this->location();
$messaging_enabled_for_current_location = $this->settings_status->is_pay_later_messaging_enabled_for_location( $location );