mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
Merge pull request #2448 from woocommerce/PCP-3202-retrieve-button-styling-properties-from-woo-commerce-checkout-block-ver-2
POC: Receive button properties from the Checkout Block (3202)
This commit is contained in:
commit
08df9c34eb
7 changed files with 97 additions and 18 deletions
3
modules/ppcp-blocks/resources/css/gateway-editor.scss
Normal file
3
modules/ppcp-blocks/resources/css/gateway-editor.scss
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
li[id^="express-payment-method-ppcp-"] div[class^="ppc-button-container-"] {
|
||||||
|
display: flex;
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
import { useEffect, useState } from '@wordpress/element';
|
import { useEffect, useState, useMemo } from '@wordpress/element';
|
||||||
import {
|
import {
|
||||||
registerExpressPaymentMethod,
|
registerExpressPaymentMethod,
|
||||||
registerPaymentMethod,
|
registerPaymentMethod,
|
||||||
|
@ -41,6 +41,7 @@ const PayPalComponent = ( {
|
||||||
shippingData,
|
shippingData,
|
||||||
isEditing,
|
isEditing,
|
||||||
fundingSource,
|
fundingSource,
|
||||||
|
buttonAttributes,
|
||||||
} ) => {
|
} ) => {
|
||||||
const { onPaymentSetup, onCheckoutFail, onCheckoutValidation } =
|
const { onPaymentSetup, onCheckoutFail, onCheckoutValidation } =
|
||||||
eventRegistration;
|
eventRegistration;
|
||||||
|
@ -614,6 +615,15 @@ const PayPalComponent = ( {
|
||||||
fundingSource
|
fundingSource
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ( typeof buttonAttributes !== 'undefined' ) {
|
||||||
|
style.height = buttonAttributes?.height
|
||||||
|
? Number( buttonAttributes.height )
|
||||||
|
: style.height;
|
||||||
|
style.borderRadius = buttonAttributes?.borderRadius
|
||||||
|
? Number( buttonAttributes.borderRadius )
|
||||||
|
: style.borderRadius;
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! paypalScriptLoaded ) {
|
if ( ! paypalScriptLoaded ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -688,20 +698,46 @@ const PayPalComponent = ( {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const BlockEditorPayPalComponent = ({ fundingSource } ) => {
|
const BlockEditorPayPalComponent = ( { fundingSource, buttonAttributes } ) => {
|
||||||
const urlParams = {
|
const urlParams = useMemo(
|
||||||
|
() => ( {
|
||||||
clientId: 'test',
|
clientId: 'test',
|
||||||
...config.scriptData.url_params,
|
...config.scriptData.url_params,
|
||||||
dataNamespace: 'ppcp-blocks-editor-paypal-buttons',
|
dataNamespace: 'ppcp-blocks-editor-paypal-buttons',
|
||||||
components: 'buttons',
|
components: 'buttons',
|
||||||
|
} ),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const style = useMemo( () => {
|
||||||
|
const configStyle = normalizeStyleForFundingSource(
|
||||||
|
config.scriptData.button.style,
|
||||||
|
fundingSource
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( buttonAttributes ) {
|
||||||
|
return {
|
||||||
|
...configStyle,
|
||||||
|
height: buttonAttributes.height
|
||||||
|
? Number( buttonAttributes.height )
|
||||||
|
: configStyle.height,
|
||||||
|
borderRadius: buttonAttributes.borderRadius
|
||||||
|
? Number( buttonAttributes.borderRadius )
|
||||||
|
: configStyle.borderRadius,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return configStyle;
|
||||||
|
}, [ fundingSource, buttonAttributes ] );
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<PayPalScriptProvider options={ urlParams }>
|
<PayPalScriptProvider options={ urlParams }>
|
||||||
<PayPalButtons
|
<PayPalButtons
|
||||||
onClick={ ( data, actions ) => {
|
className={ `ppc-button-container-${ fundingSource }` }
|
||||||
return false;
|
|
||||||
} }
|
|
||||||
fundingSource={ fundingSource }
|
fundingSource={ fundingSource }
|
||||||
|
style={ style }
|
||||||
|
forceReRender={ [ buttonAttributes || {} ] }
|
||||||
|
onClick={ () => false }
|
||||||
/>
|
/>
|
||||||
</PayPalScriptProvider>
|
</PayPalScriptProvider>
|
||||||
);
|
);
|
||||||
|
@ -787,7 +823,7 @@ if ( block_enabled ) {
|
||||||
name: config.id,
|
name: config.id,
|
||||||
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
|
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
|
||||||
content: <PayPalComponent isEditing={ false } />,
|
content: <PayPalComponent isEditing={ false } />,
|
||||||
edit: <BlockEditorPayPalComponent fundingSource={ 'paypal' }/>,
|
edit: <BlockEditorPayPalComponent fundingSource={ 'paypal' } />,
|
||||||
ariaLabel: config.title,
|
ariaLabel: config.title,
|
||||||
canMakePayment: () => {
|
canMakePayment: () => {
|
||||||
return true;
|
return true;
|
||||||
|
@ -819,9 +855,11 @@ if ( block_enabled ) {
|
||||||
fundingSource={ fundingSource }
|
fundingSource={ fundingSource }
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
edit: <BlockEditorPayPalComponent
|
edit: (
|
||||||
|
<BlockEditorPayPalComponent
|
||||||
fundingSource={ fundingSource }
|
fundingSource={ fundingSource }
|
||||||
/>,
|
/>
|
||||||
|
),
|
||||||
ariaLabel: config.title,
|
ariaLabel: config.title,
|
||||||
canMakePayment: async () => {
|
canMakePayment: async () => {
|
||||||
if ( ! paypalScriptPromise ) {
|
if ( ! paypalScriptPromise ) {
|
||||||
|
@ -845,6 +883,7 @@ if ( block_enabled ) {
|
||||||
},
|
},
|
||||||
supports: {
|
supports: {
|
||||||
features,
|
features,
|
||||||
|
style: [ 'height', 'borderRadius' ],
|
||||||
},
|
},
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,6 +126,23 @@ class BlocksModule implements ServiceModule, ExtendingModule, ExecutableModule {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Enqueue editor styles.
|
||||||
|
add_action(
|
||||||
|
'enqueue_block_editor_assets',
|
||||||
|
static function () use ( $c ) {
|
||||||
|
$module_url = $c->get( 'blocks.url' );
|
||||||
|
$asset_version = $c->get( 'ppcp.asset-version' );
|
||||||
|
|
||||||
|
wp_register_style(
|
||||||
|
'wc-ppcp-blocks-editor',
|
||||||
|
untrailingslashit( $module_url ) . '/assets/css/gateway-editor.css',
|
||||||
|
array(),
|
||||||
|
$asset_version
|
||||||
|
);
|
||||||
|
wp_enqueue_style( 'wc-ppcp-blocks-editor' );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
add_filter(
|
add_filter(
|
||||||
'woocommerce_paypal_payments_sdk_components_hook',
|
'woocommerce_paypal_payments_sdk_components_hook',
|
||||||
function( array $components ) {
|
function( array $components ) {
|
||||||
|
|
|
@ -11,7 +11,8 @@ module.exports = {
|
||||||
entry: {
|
entry: {
|
||||||
'checkout-block': path.resolve('./resources/js/checkout-block.js'),
|
'checkout-block': path.resolve('./resources/js/checkout-block.js'),
|
||||||
'advanced-card-checkout-block': path.resolve('./resources/js/advanced-card-checkout-block.js'),
|
'advanced-card-checkout-block': path.resolve('./resources/js/advanced-card-checkout-block.js'),
|
||||||
"gateway": path.resolve('./resources/css/gateway.scss')
|
"gateway": path.resolve('./resources/css/gateway.scss'),
|
||||||
|
"gateway-editor": path.resolve('./resources/css/gateway-editor.scss')
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
path: path.resolve(__dirname, 'assets/'),
|
path: path.resolve(__dirname, 'assets/'),
|
||||||
|
|
|
@ -32,7 +32,7 @@
|
||||||
|
|
||||||
@media (min-width: 1200px) {
|
@media (min-width: 1200px) {
|
||||||
float: right;
|
float: right;
|
||||||
margin-top: -300px;
|
margin-top: -250px;
|
||||||
max-width: calc(100vw - 850px);
|
max-width: calc(100vw - 850px);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1843,6 +1843,24 @@ return array(
|
||||||
unset( $button_locations['mini-cart'] );
|
unset( $button_locations['mini-cart'] );
|
||||||
return array_keys( $button_locations );
|
return array_keys( $button_locations );
|
||||||
},
|
},
|
||||||
|
'wcgateway.button.recommended-styling-notice' => static function ( ContainerInterface $container ) : string {
|
||||||
|
if ( CartCheckoutDetector::has_block_checkout() ) {
|
||||||
|
$block_checkout_page_string_html = '<a href="' . esc_url( wc_get_page_permalink( 'checkout' ) ) . '">' . __( 'Checkout block', 'woocommerce-paypal-payments' ) . '</a>';
|
||||||
|
} else {
|
||||||
|
$block_checkout_page_string_html = __( 'Checkout block', 'woocommerce-paypal-payments' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$notice_content = sprintf(
|
||||||
|
/* translators: %1$s: URL to the Checkout edit page. */
|
||||||
|
__(
|
||||||
|
'<span class="highlight">Important:</span> The <code>Cart</code> & <code>Express Checkout</code> <strong>Smart Button Stylings</strong> may be controlled by the %1$s configuration.',
|
||||||
|
'woocommerce-paypal-payments'
|
||||||
|
),
|
||||||
|
$block_checkout_page_string_html
|
||||||
|
);
|
||||||
|
|
||||||
|
return '<div class="ppcp-notice ppcp-notice-warning"><p>' . $notice_content . '</p></div>';
|
||||||
|
},
|
||||||
'wcgateway.settings.pay-later.messaging-locations' => static function( ContainerInterface $container ): array {
|
'wcgateway.settings.pay-later.messaging-locations' => static function( ContainerInterface $container ): array {
|
||||||
$button_locations = $container->get( 'wcgateway.button.locations' );
|
$button_locations = $container->get( 'wcgateway.button.locations' );
|
||||||
unset( $button_locations['mini-cart'] );
|
unset( $button_locations['mini-cart'] );
|
||||||
|
|
|
@ -88,6 +88,7 @@ return function ( ContainerInterface $container, array $fields ): array {
|
||||||
'type' => 'checkbox',
|
'type' => 'checkbox',
|
||||||
'label' => __( 'Customize smart button style per location', 'woocommerce-paypal-payments' ),
|
'label' => __( 'Customize smart button style per location', 'woocommerce-paypal-payments' ),
|
||||||
'default' => false,
|
'default' => false,
|
||||||
|
'description' => $container->has( 'wcgateway.button.recommended-styling-notice' ) ? $container->get( 'wcgateway.button.recommended-styling-notice' ) : '',
|
||||||
'screens' => array( State::STATE_START, State::STATE_ONBOARDED ),
|
'screens' => array( State::STATE_START, State::STATE_ONBOARDED ),
|
||||||
'requirements' => array(),
|
'requirements' => array(),
|
||||||
'gateway' => 'paypal',
|
'gateway' => 'paypal',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue