Merge pull request #2714 from woocommerce/PCP-3347-rework-location-settings

Use basic redirect gateway when checkout smart buttons disabled
This commit is contained in:
Emili Castells 2024-10-24 11:10:43 +02:00 committed by GitHub
commit 9f3e8a0723
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 115 additions and 60 deletions

View file

@ -113,7 +113,7 @@ class BlocksPaymentMethod extends AbstractPaymentMethodType {
'id' => $this->name,
'title' => $paypal_data['title'], // TODO : see if we should use another.
'description' => $paypal_data['description'], // TODO : see if we should use another.
'enabled' => $paypal_data['enabled'], // This button is enabled when PayPal buttons are.
'enabled' => $paypal_data['smartButtonsEnabled'], // This button is enabled when PayPal buttons are.
'scriptData' => $script_data,
);
}

View file

@ -739,11 +739,8 @@ if ( cartHasSubscriptionProducts( config.scriptData ) ) {
features.push( 'subscriptions' );
}
if ( block_enabled && config.enabled ) {
if (
( config.addPlaceOrderMethod || config.usePlaceOrder ) &&
! config.scriptData.continuation
) {
if ( block_enabled ) {
if ( config.placeOrderEnabled && ! config.scriptData.continuation ) {
let descriptionElement = (
<div
dangerouslySetInnerHTML={ { __html: config.description } }
@ -776,7 +773,7 @@ if ( block_enabled && config.enabled ) {
placeOrderButtonLabel: config.placeOrderButtonText,
ariaLabel: config.title,
canMakePayment: () => {
return config.enabled;
return true;
},
supports: {
features,
@ -798,7 +795,7 @@ if ( block_enabled && config.enabled ) {
features: [ ...features, 'ppcp_continuation' ],
},
} );
} else if ( ! config.usePlaceOrder ) {
} else if ( config.smartButtonsEnabled ) {
for ( const fundingSource of [
'paypal',
...config.enabledFundingSources,

View file

@ -47,6 +47,7 @@ return array(
$container->get( 'blocks.settings.final_review_enabled' ),
$container->get( 'session.cancellation.view' ),
$container->get( 'session.handler' ),
$container->get( 'wc-subscriptions.helper' ),
$container->get( 'blocks.add-place-order-method' ),
$container->get( 'wcgateway.use-place-order-button' ),
$container->get( 'wcgateway.place-order-button-text' ),

View file

@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
/**
* Class PayPalPaymentMethod
@ -87,6 +88,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
*/
private $session_handler;
/**
* The Subscription Helper.
*
* @var SubscriptionHelper
*/
private $subscription_helper;
/**
* Whether to create a non-express method with the standard "Place order" button.
*
@ -141,6 +149,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
* @param bool $final_review_enabled Whether the final review is enabled.
* @param CancelView $cancellation_view The cancellation view.
* @param SessionHandler $session_handler The Session handler.
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param bool $add_place_order_method Whether to create a non-express method with the standard "Place order" button.
* @param bool $use_place_order Whether to use the standard "Place order" button instead of PayPal buttons.
* @param string $place_order_button_text The text for the standard "Place order" button.
@ -158,6 +167,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
bool $final_review_enabled,
CancelView $cancellation_view,
SessionHandler $session_handler,
SubscriptionHelper $subscription_helper,
bool $add_place_order_method,
bool $use_place_order,
string $place_order_button_text,
@ -175,6 +185,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
$this->final_review_enabled = $final_review_enabled;
$this->cancellation_view = $cancellation_view;
$this->session_handler = $session_handler;
$this->subscription_helper = $subscription_helper;
$this->add_place_order_method = $add_place_order_method;
$this->use_place_order = $use_place_order;
$this->place_order_button_text = $place_order_button_text;
@ -195,9 +206,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
// Do not load when definitely not needed,
// but we still need to check the locations later and handle in JS
// because has_block cannot be called here (too early).
return $this->plugin_settings->has( 'enabled' ) && $this->plugin_settings->get( 'enabled' )
&& ( $this->settings_status->is_smart_button_enabled_for_location( 'checkout-block-express' ) ||
$this->settings_status->is_smart_button_enabled_for_location( 'cart-block' ) );
return $this->plugin_settings->has( 'enabled' ) && $this->plugin_settings->get( 'enabled' );
}
/**
@ -245,15 +254,19 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
);
}
$smart_buttons_enabled = ! $this->use_place_order
&& $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ?? 'block-checkout' );
$place_order_enabled = ( $this->use_place_order || $this->add_place_order_method )
&& ! $this->subscription_helper->cart_contains_subscription();
return array(
'id' => $this->gateway->id,
'title' => $this->gateway->title,
'description' => $this->gateway->description,
'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ?? 'checkout' ),
'smartButtonsEnabled' => $smart_buttons_enabled,
'placeOrderEnabled' => $place_order_enabled,
'fundingSource' => $this->session_handler->funding_source(),
'finalReviewEnabled' => $this->final_review_enabled,
'addPlaceOrderMethod' => $this->add_place_order_method,
'usePlaceOrder' => $this->use_place_order,
'placeOrderButtonText' => $this->place_order_button_text,
'placeOrderButtonDescription' => $this->place_order_button_description,
'enabledFundingSources' => $funding_sources,

View file

@ -37,6 +37,7 @@ use WooCommerce\PayPalCommerce\Button\Helper\MessagesApply;
use WooCommerce\PayPalCommerce\Button\Helper\ThreeDSecure;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
return array(
'button.client_id' => static function ( ContainerInterface $container ): string {
@ -101,12 +102,20 @@ return array(
return $obj->get_context();
},
'button.smart-button' => static function ( ContainerInterface $container ): SmartButtonInterface {
$state = $container->get( 'onboarding.state' );
if ( $container->get( 'wcgateway.use-place-order-button' )
&& in_array( $container->get( 'button.context' ), array( 'checkout', 'pay-now' ), true )
) {
return new DisabledSmartButton();
$context = $container->get( 'button.context' );
$settings_status = $container->get( 'wcgateway.settings.status' );
assert( $settings_status instanceof SettingsStatus );
if ( in_array( $context, array( 'checkout', 'pay-now' ), true ) ) {
if ( $container->get( 'wcgateway.use-place-order-button' )
|| ! $settings_status->is_smart_button_enabled_for_location( $context )
) {
return new DisabledSmartButton();
}
}
$state = $container->get( 'onboarding.state' );
if ( $state->current_state() !== State::STATE_ONBOARDED ) {
return new DisabledSmartButton();
}
@ -125,7 +134,6 @@ return array(
$messages_apply = $container->get( 'button.helper.messages-apply' );
$environment = $container->get( 'onboarding.environment' );
$payment_token_repository = $container->get( 'vaulting.repository.payment-token' );
$settings_status = $container->get( 'wcgateway.settings.status' );
return new SmartButton(
$container->get( 'button.url' ),
$container->get( 'ppcp.asset-version' ),

View file

@ -107,7 +107,7 @@ class BlocksPaymentMethod extends AbstractPaymentMethodType {
'id' => $this->name,
'title' => $paypal_data['title'], // See if we should use another.
'description' => $paypal_data['description'], // See if we should use another.
'enabled' => $paypal_data['enabled'], // This button is enabled when PayPal buttons are.
'enabled' => $paypal_data['smartButtonsEnabled'], // This button is enabled when PayPal buttons are.
'scriptData' => $this->button->script_data(),
);
}

View file

@ -1,3 +1,8 @@
import {
setVisible,
setVisibleByClass,
} from '../../../ppcp-button/resources/js/modules/Helper/Hiding';
document.addEventListener( 'DOMContentLoaded', () => {
const payLaterMessagingSelectableLocations = [
'product',
@ -216,6 +221,18 @@ document.addEventListener( 'DOMContentLoaded', () => {
replace();
};
const hideElements = ( selectorGroup ) => {
selectorGroup.forEach( ( selector ) =>
setVisibleByClass( selector, false, 'hide' )
);
};
const showElements = ( selectorGroup ) => {
selectorGroup.forEach( ( selector ) =>
setVisibleByClass( selector, true, 'hide' )
);
};
const toggleInputsBySelectedLocations = (
stylingPerSelector,
locationsSelector,
@ -226,30 +243,30 @@ document.addEventListener( 'DOMContentLoaded', () => {
const payLaterMessagingEnabled = document.querySelector(
payLaterMessagingEnabledSelector
);
const stylingPerElement = document.querySelector( stylingPerSelector );
const locationsElement = document.querySelector( locationsSelector );
const stylingPerElementWrapper = stylingPerElement?.closest( 'tr' );
const stylingPerElementWrapperSelector =
'#' + stylingPerElementWrapper?.getAttribute( 'id' );
const stylingPerElement = document.querySelector( stylingPerSelector );
if ( ! stylingPerElement ) {
return;
}
const stylingPerElementWrapper = stylingPerElement.closest( 'tr' );
const toggleElementsBySelectedLocations = () => {
stylingPerElementWrapper.style.display = '';
const selectedLocations = getSelectedLocations( locationsSelector );
const emptySmartButtonLocationMessage = jQuery(
'.ppcp-empty-smart-button-location'
setVisibleByClass(
stylingPerElementWrapper,
selectedLocations.length > 0,
'hide'
);
if ( selectedLocations.length === 0 ) {
hideElements(
groupToHideOnChecked.concat(
stylingPerElementWrapperSelector
)
hideElements( groupToHideOnChecked );
const emptySmartButtonLocationMessage = document.querySelector(
'.ppcp-empty-smart-button-location'
);
if ( emptySmartButtonLocationMessage.length === 0 ) {
if ( ! emptySmartButtonLocationMessage ) {
jQuery(
PayPalCommerceSettings.empty_smart_button_location_message
).insertAfter(
@ -277,11 +294,11 @@ document.addEventListener( 'DOMContentLoaded', () => {
);
groupToShowOnChecked.forEach( ( element ) => {
if ( inputSelectors.includes( element ) ) {
document.querySelector( element ).style.display = '';
return;
}
document.querySelector( element ).style.display = 'none';
setVisibleByClass(
element,
inputSelectors.includes( element ),
'hide'
);
} );
if ( inputType === 'messages' ) {
@ -289,18 +306,6 @@ document.addEventListener( 'DOMContentLoaded', () => {
}
};
const hideElements = ( selectroGroup ) => {
selectroGroup.forEach( ( elementToHide ) => {
document.querySelector( elementToHide ).style.display = 'none';
} );
};
const showElements = ( selectroGroup ) => {
selectroGroup.forEach( ( elementToShow ) => {
document.querySelector( elementToShow ).style.display = '';
} );
};
groupToggle( stylingPerSelector, groupToShowOnChecked );
toggleElementsBySelectedLocations();
@ -327,7 +332,7 @@ document.addEventListener( 'DOMContentLoaded', () => {
} );
// We need to use jQuery here as the select might be a select2 element, which doesn't use native events.
jQuery( locationsElement ).on( 'change', function () {
jQuery( locationsSelector ).on( 'change', function () {
const emptySmartButtonLocationMessage = jQuery(
'.ppcp-empty-smart-button-location'
);
@ -457,6 +462,38 @@ document.addEventListener( 'DOMContentLoaded', () => {
}
};
/**
* Hide the subscription settings when smart buttons are disabled for checkout,
* since the basic redirect gateway is disabled for subscriptions.
*/
const initSettingsHidingForPlaceOrderGateway = () => {
const selectors = [
'#field-paypal_saved_payments',
'#field-subscriptions_mode',
'#field-vault_enabled',
];
const updateSettingsVisibility = () => {
const selectedLocations = getSelectedLocations(
smartButtonLocationsSelect
);
const hasCheckoutSmartButtons =
selectedLocations.includes( 'checkout' ) ||
selectedLocations.includes( 'checkout-block-express' );
selectors.forEach( ( selector ) => {
setVisibleByClass( selector, hasCheckoutSmartButtons, 'hide' );
} );
};
updateSettingsVisibility();
jQuery( smartButtonLocationsSelect ).on(
'change',
updateSettingsVisibility
);
};
( () => {
removeDisabledCardIcons(
'select[name="ppcp[disable_cards][]"]',
@ -488,6 +525,8 @@ document.addEventListener( 'DOMContentLoaded', () => {
toggleMessagingEnabled();
initSettingsHidingForPlaceOrderGateway();
groupToggle( '#ppcp-vault_enabled', [
'#field-subscription_behavior_when_vault_fails',
] );

View file

@ -117,7 +117,7 @@ class OnboardingAssets {
array(
'empty_smart_button_location_message' => sprintf(
'<p class="description ppcp-empty-smart-button-location">%1$s</p>',
__( 'Note: If no button location is selected, the PayPal gateway will not be available.', 'woocommerce-paypal-payments' )
__( 'Note: PayPal buttons and advanced payment features (Alternative Payment Methods, Subscriptions, etc.) are unavailable if no Smart Button Location is configured.', 'woocommerce-paypal-payments' )
),
)
);

View file

@ -97,7 +97,7 @@ class DisableGateways {
if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
unset( $methods[ CardButtonGateway::ID ] );
if ( ! $this->session_handler->order() && is_checkout() ) {
if ( $this->subscription_helper->cart_contains_subscription() ) {
unset( $methods[ PayPalGateway::ID ] );
}
}

View file

@ -366,7 +366,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
'type' => 'checkbox',
'desc_tip' => true,
'description' => __( 'In order to use PayPal or Advanced Card Processing, you need to enable the Gateway.', 'woocommerce-paypal-payments' ),
'label' => __( 'Enable PayPal features for your store', 'woocommerce-paypal-payments' ),
'label' => __( 'Enable the PayPal gateway and more features for your store.', 'woocommerce-paypal-payments' ),
'default' => 'no',
),
'ppcp' => array(

View file

@ -226,14 +226,11 @@ return function ( ContainerInterface $container, array $fields ): array {
'description' => sprintf(
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
__(
'Customize the appearance of the PayPal smart buttons on the %1$sClassic Checkout page%2$s.
%3$sCheckout Buttons must be enabled to display the PayPal gateway on the Checkout page.
',
'Customize the appearance of the PayPal smart buttons on the %1$sClassic Checkout page%2$s.',
'woocommerce-paypal-payments'
),
'<a href="https://woocommerce.com/document/woocommerce-paypal-payments/#button-on-checkout" target="_blank">',
'</a>',
'<br />'
'</a>'
),
'type' => 'ppcp-heading',
'screens' => array(