Do not show broken card button gateway when no checkout location

Removes the card button gateway together with the PayPal gateway when disabled in checkout (since it gets broken anyway).
Also refactored the admin notice about disabled gateway to add a message about disabled location.
This commit is contained in:
Alex P 2023-05-08 09:51:32 +03:00
parent af5d33dc78
commit c0368e9ada
No known key found for this signature in database
GPG key ID: 54487A734A204D71
3 changed files with 68 additions and 25 deletions

View file

@ -223,7 +223,8 @@ return array(
$container->get( 'onboarding.state' ), $container->get( 'onboarding.state' ),
$container->get( 'wcgateway.settings' ), $container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.is-wc-payments-page' ), $container->get( 'wcgateway.is-wc-payments-page' ),
$container->get( 'wcgateway.is-ppcp-settings-page' ) $container->get( 'wcgateway.is-ppcp-settings-page' ),
$container->get( 'wcgateway.settings.status' )
); );
}, },
'wcgateway.notice.authorize-order-action' => 'wcgateway.notice.authorize-order-action' =>

View file

@ -82,8 +82,11 @@ class DisableGateways {
unset( $methods[ CreditCardGateway::ID ] ); unset( $methods[ CreditCardGateway::ID ] );
} }
if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) && ! $this->session_handler->order() && is_checkout() ) { if ( ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
unset( $methods[ PayPalGateway::ID ] ); unset( $methods[ CardButtonGateway::ID ] );
if ( ! $this->session_handler->order() && is_checkout() ) {
unset( $methods[ PayPalGateway::ID ] );
}
} }
if ( ! $this->needs_to_disable_gateways() ) { if ( ! $this->needs_to_disable_gateways() ) {

View file

@ -13,11 +13,16 @@ use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
/** /**
* Creates the admin message about the gateway being enabled without the PayPal gateway. * Creates the admin message about the gateway being enabled without the PayPal gateway.
*/ */
class GatewayWithoutPayPalAdminNotice { class GatewayWithoutPayPalAdminNotice {
private const NOTICE_OK = '';
private const NOTICE_DISABLED_GATEWAY = 'disabled_gateway';
private const NOTICE_DISABLED_LOCATION = 'disabled_location';
/** /**
* The gateway ID. * The gateway ID.
* *
@ -53,27 +58,37 @@ class GatewayWithoutPayPalAdminNotice {
*/ */
private $is_ppcp_settings_page; private $is_ppcp_settings_page;
/**
* The Settings status helper.
*
* @var SettingsStatus|null
*/
protected $settings_status;
/** /**
* ConnectAdminNotice constructor. * ConnectAdminNotice constructor.
* *
* @param string $id The gateway ID. * @param string $id The gateway ID.
* @param State $state The state. * @param State $state The state.
* @param ContainerInterface $settings The settings. * @param ContainerInterface $settings The settings.
* @param bool $is_payments_page Whether the current page is the WC payment page. * @param bool $is_payments_page Whether the current page is the WC payment page.
* @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page. * @param bool $is_ppcp_settings_page Whether the current page is the PPCP settings page.
* @param SettingsStatus|null $settings_status The Settings status helper.
*/ */
public function __construct( public function __construct(
string $id, string $id,
State $state, State $state,
ContainerInterface $settings, ContainerInterface $settings,
bool $is_payments_page, bool $is_payments_page,
bool $is_ppcp_settings_page bool $is_ppcp_settings_page,
?SettingsStatus $settings_status = null
) { ) {
$this->id = $id; $this->id = $id;
$this->state = $state; $this->state = $state;
$this->settings = $settings; $this->settings = $settings;
$this->is_payments_page = $is_payments_page; $this->is_payments_page = $is_payments_page;
$this->is_ppcp_settings_page = $is_ppcp_settings_page; $this->is_ppcp_settings_page = $is_ppcp_settings_page;
$this->settings_status = $settings_status;
} }
/** /**
@ -82,8 +97,25 @@ class GatewayWithoutPayPalAdminNotice {
* @return Message|null * @return Message|null
*/ */
public function message(): ?Message { public function message(): ?Message {
if ( ! $this->should_display() ) { $notice_type = $this->check();
return null;
switch ( $notice_type ) {
case self::NOTICE_DISABLED_GATEWAY:
/* translators: %1$s the gateway name, %2$s URL. */
$text = __(
'%1$s cannot be used without the PayPal gateway. <a href="%2$s">Enable the PayPal gateway</a>.',
'woocommerce-paypal-payments'
);
break;
case self::NOTICE_DISABLED_LOCATION:
/* translators: %1$s the gateway name, %2$s URL. */
$text = __(
'%1$s cannot be used without enabling the Checkout location for the PayPal gateway. <a href="%2$s">Enable the Checkout location</a>.',
'woocommerce-paypal-payments'
);
break;
default:
return null;
} }
$gateway = $this->get_gateway(); $gateway = $this->get_gateway();
@ -94,11 +126,7 @@ class GatewayWithoutPayPalAdminNotice {
$name = $gateway->get_method_title(); $name = $gateway->get_method_title();
$message = sprintf( $message = sprintf(
/* translators: %1$s the gateway name, %2$s URL. */ $text,
__(
'%1$s cannot be used without the PayPal gateway. <a href="%2$s">Enable the PayPal gateway</a>.',
'woocommerce-paypal-payments'
),
$name, $name,
admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ) admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' )
); );
@ -106,22 +134,33 @@ class GatewayWithoutPayPalAdminNotice {
} }
/** /**
* Whether the message should be displayed. * Checks whether one of the messages should be displayed.
* *
* @return bool * @return string One of the NOTICE_* constants.
*/ */
protected function should_display(): bool { protected function check(): string {
if ( State::STATE_ONBOARDED !== $this->state->current_state() || if ( State::STATE_ONBOARDED !== $this->state->current_state() ||
( ! $this->is_payments_page && ! $this->is_ppcp_settings_page ) ) { ( ! $this->is_payments_page && ! $this->is_ppcp_settings_page ) ) {
return false; return self::NOTICE_OK;
}
if ( $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' ) ) {
return false;
} }
$gateway = $this->get_gateway(); $gateway = $this->get_gateway();
$gateway_enabled = $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) );
return $gateway && wc_string_to_bool( $gateway->get_option( 'enabled' ) ); if ( ! $gateway_enabled ) {
return self::NOTICE_OK;
}
$paypal_enabled = $this->settings->has( 'enabled' ) && $this->settings->get( 'enabled' );
if ( ! $paypal_enabled ) {
return self::NOTICE_DISABLED_GATEWAY;
}
if ( $this->settings_status && ! $this->settings_status->is_smart_button_enabled_for_location( 'checkout' ) ) {
return self::NOTICE_DISABLED_LOCATION;
}
return self::NOTICE_OK;
} }
/** /**