Merge pull request #1358 from woocommerce/pcp-1583-card-button-no-location

Do not show broken card button gateway when no checkout location
This commit is contained in:
Emili Castells 2023-06-27 11:37:18 +02:00 committed by GitHub
commit 70412a8f27
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 25 deletions

View file

@ -230,7 +230,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,9 +82,12 @@ 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[ CardButtonGateway::ID ] );
if ( ! $this->session_handler->order() && is_checkout() ) {
unset( $methods[ PayPalGateway::ID ] ); unset( $methods[ PayPalGateway::ID ] );
} }
}
if ( ! $this->needs_to_disable_gateways() ) { if ( ! $this->needs_to_disable_gateways() ) {
return $methods; return $methods;

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,6 +58,13 @@ 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.
* *
@ -61,19 +73,22 @@ class GatewayWithoutPayPalAdminNotice {
* @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,7 +97,24 @@ class GatewayWithoutPayPalAdminNotice {
* @return Message|null * @return Message|null
*/ */
public function message(): ?Message { public function message(): ?Message {
if ( ! $this->should_display() ) { $notice_type = $this->check();
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; return null;
} }
@ -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;
} }
/** /**