Merge pull request #2464 from woocommerce/PCP-3374-google-pay-button-not-displayed-in-the-word-press-editor

Fix the Google Pay button in the block Checkout in the editor (3374)
This commit is contained in:
Emili Castells 2024-10-03 10:39:01 +02:00 committed by GitHub
commit 8592213b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 100 additions and 10 deletions

View file

@ -7,15 +7,25 @@ import { getCurrentPaymentMethod } from './CheckoutMethodState';
import { v4 as uuidv4 } from 'uuid';
// This component may be used by multiple modules. This assures that options are shared between all instances.
const options = ( window.ppcpWidgetBuilder = window.ppcpWidgetBuilder || {
isLoading: false,
onLoadedCallbacks: [],
onErrorCallbacks: [],
} );
const scriptOptionsMap = {};
const getNamespaceOptions = ( namespace ) => {
if ( ! scriptOptionsMap[ namespace ] ) {
scriptOptionsMap[ namespace ] = {
isLoading: false,
onLoadedCallbacks: [],
onErrorCallbacks: [],
};
}
return scriptOptionsMap[ namespace ];
};
export const loadPaypalScript = ( config, onLoaded, onError = null ) => {
// If PayPal is already loaded call the onLoaded callback and return.
if ( typeof paypal !== 'undefined' ) {
const dataNamespace = config?.data_namespace || '';
const options = getNamespaceOptions( dataNamespace );
// If PayPal is already loaded for this namespace, call the onLoaded callback and return.
if ( typeof window.paypal !== 'undefined' && ! dataNamespace ) {
onLoaded();
return;
}
@ -90,6 +100,11 @@ export const loadPaypalScript = ( config, onLoaded, onError = null ) => {
scriptOptions[ 'data-user-id-token' ] = userIdToken;
}
// Adds data-namespace to script options.
if ( dataNamespace ) {
scriptOptions.dataNamespace = dataNamespace;
}
// Load PayPal script
loadScript( scriptOptions ).then( callback ).catch( errorCallback );
};