Fix the fallback placement for the Cart - Pay later messaging block

This commit is contained in:
Daniel Dudzic 2024-04-25 10:55:43 +02:00
parent 287a4a2819
commit c34fc7d8b9
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
4 changed files with 30 additions and 13 deletions

View file

@ -18,6 +18,7 @@
"lock": {
"type": "object",
"default": {
"remove": true,
"move": false
}
}

View file

@ -3,8 +3,30 @@
const { select, dispatch, subscribe } = wp.data;
const getBlocks = () => select('core/block-editor').getBlocks() || [];
// Store the initial list of block client IDs
let blockList = getBlocks().map(block => block.clientId);
const { addFilter } = wp.hooks;
const { assign } = lodash;
// We need to make sure the block is unlocked so that it doesn't get automatically inserted as the last block
addFilter(
'blocks.registerBlockType',
'woocommerce-paypal-payments/modify-cart-paylater-messages',
(settings, name) => {
if (name === 'woocommerce-paypal-payments/cart-paylater-messages') {
const newAttributes = assign({}, settings.attributes, {
lock: assign({}, settings.attributes.lock, {
default: assign({}, settings.attributes.lock.default, {
remove: false
})
})
});
return assign({}, settings, {
attributes: newAttributes
});
}
return settings;
}
);
/**
* Subscribes to changes in the block editor, specifically checking for the presence of 'woocommerce/cart'.
@ -91,11 +113,9 @@
dispatch('core/block-editor').insertBlock(newBlock, parentBlock.innerBlocks.length - offset, parentBlock.clientId);
// Lock the block after it has been inserted
setTimeout(() => {
dispatch('core/block-editor').updateBlockAttributes(newBlock.clientId, {
lock: { remove: true }
});
}, 1000);
dispatch('core/block-editor').updateBlockAttributes(newBlock.clientId, {
lock: { remove: true }
});
}
/**

View file

@ -9,8 +9,6 @@ import { registerBlockType } from '@wordpress/blocks';
import Edit from './edit';
import metadata from './block.json';
const { underTotalsPlacementEnabled } = window.PcpCartPayLaterBlock;
const paypalIcon = (
<svg width="584.798" height="720" viewBox="0 0 154.728 190.5">
<g transform="translate(898.192 276.071)">
@ -33,8 +31,6 @@ const paypalIcon = (
</svg>
);
metadata.attributes.lock.default.remove = !Boolean(underTotalsPlacementEnabled);
registerBlockType(metadata, {
icon: paypalIcon,
edit: Edit,

View file

@ -24,7 +24,7 @@ class PayLaterWCBlocksUtils {
public static function insert_before_last_div( string $block_content, string $content_to_insert ): string {
$last_index = strrpos( $block_content, '</div>' );
if ( $last_index !== false ) {
if ( false !== $last_index ) {
$block_content = substr_replace( $block_content, $content_to_insert, $last_index, 0 );
}
@ -65,7 +65,7 @@ class PayLaterWCBlocksUtils {
$cart_express_payment_block = '<div data-block-name="woocommerce/cart-express-payment-block" class="wp-block-woocommerce-cart-express-payment-block"></div>';
if ( false !== $paylater_message_block ) {
if ( $is_under_cart_totals_placement_enabled && 'cart' === $context ) {
if ( $is_under_cart_totals_placement_enabled && $context === 'cart' ) {
return self::insert_before_opening_div( $block_content, $paylater_message_block, $cart_express_payment_block );
} else {
return self::insert_before_last_div( $block_content, $paylater_message_block );