Get button locations from smart_button_locations setting

This commit is contained in:
emilicastells 2023-02-09 16:35:53 +01:00
parent 664a53dd36
commit 88d85ca87d
No known key found for this signature in database
GPG key ID: 1520C07081754570
2 changed files with 71 additions and 57 deletions

View file

@ -1333,39 +1333,6 @@ return array(
return $enabled_ppcp_gateways;
},
'wcgateway.is-paypal-continuation' => static function ( ContainerInterface $container ): bool {
$session_handler = $container->get( 'session.handler' );
assert( $session_handler instanceof SessionHandler );
$order = $session_handler->order();
if ( ! $order ) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.
}
if ( 'card' === $session_handler->funding_source() ) {
return false; // Ignore for card buttons.
}
return true;
},
'wcgateway.current-context' => static function ( ContainerInterface $container ): string {
$context = 'mini-cart';
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
$context = 'product';
}
if ( is_cart() ) {
$context = 'cart';
}
if ( is_checkout() && ! $container->get( 'wcgateway.is-paypal-continuation' ) ) {
$context = 'checkout';
}
if ( is_checkout_pay_page() ) {
$context = 'pay-now';
}
return $context;
},
'wcgateway.is-fraudnet-enabled' => static function ( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
@ -1380,7 +1347,7 @@ return array(
$container->get( 'onboarding.environment' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'wcgateway.enabled-ppcp-gateways' ),
$container->get( 'wcgateway.current-context' ),
$container->get( 'session.handler' ),
$container->get( 'wcgateway.is-fraudnet-enabled' )
);
},

View file

@ -10,6 +10,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
@ -63,11 +65,11 @@ class FraudNetAssets {
protected $enabled_ppcp_gateways;
/**
* The current context.
* The session handler
*
* @var string
* @var SessionHandler
*/
protected $context;
protected $session_handler;
/**
* True if FraudNet support is enabled in settings, otherwise false.
@ -79,14 +81,14 @@ class FraudNetAssets {
/**
* Assets constructor.
*
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param FraudNet $fraud_net The FraudNet entity.
* @param Environment $environment The environment.
* @param Settings $settings The Settings.
* @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways.
* @param string $context The current context.
* @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false.
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param FraudNet $fraud_net The FraudNet entity.
* @param Environment $environment The environment.
* @param Settings $settings The Settings.
* @param string[] $enabled_ppcp_gateways The list of enabled PayPal gateways.
* @param SessionHandler $session_handler The session handler.
* @param bool $is_fraudnet_enabled true if FraudNet support is enabled in settings, otherwise false.
*/
public function __construct(
string $module_url,
@ -95,7 +97,7 @@ class FraudNetAssets {
Environment $environment,
Settings $settings,
array $enabled_ppcp_gateways,
string $context,
SessionHandler $session_handler,
bool $is_fraudnet_enabled
) {
$this->module_url = $module_url;
@ -104,7 +106,7 @@ class FraudNetAssets {
$this->environment = $environment;
$this->settings = $settings;
$this->enabled_ppcp_gateways = $enabled_ppcp_gateways;
$this->context = $context;
$this->session_handler = $session_handler;
$this->is_fraudnet_enabled = $is_fraudnet_enabled;
}
@ -151,7 +153,7 @@ class FraudNetAssets {
$is_pui_gateway_enabled = in_array( PayUponInvoiceGateway::ID, $this->enabled_ppcp_gateways, true );
$is_only_standard_gateway_enabled = $this->enabled_ppcp_gateways === array( PayPalGateway::ID );
if ( $this->context !== 'checkout' || $is_only_standard_gateway_enabled ) {
if ( $this->context() !== 'checkout' || $is_only_standard_gateway_enabled ) {
return $this->is_fraudnet_enabled && $this->are_buttons_enabled_for_context();
}
@ -168,19 +170,64 @@ class FraudNetAssets {
if ( ! in_array( PayPalGateway::ID, $this->enabled_ppcp_gateways, true ) ) {
return false;
}
$location_prefix = $this->context === 'checkout' ? '' : "{$this->context}_";
$setting_name = "button_{$location_prefix}enabled";
$buttons_enabled_for_context = $this->settings->has( $setting_name ) && $this->settings->get( $setting_name );
if ( $this->context === 'product' ) {
return $buttons_enabled_for_context || $this->settings->has( 'mini-cart' ) && $this->settings->get( 'mini-cart' );
try {
$button_locations = $this->settings->get( 'smart_button_locations' );
} catch ( NotFoundException $exception ) {
return false;
}
if ( $this->context === 'pay-now' ) {
if ( $this->context() === 'pay-now' ) {
return true;
}
return $buttons_enabled_for_context;
if ( $this->context() === 'product' ) {
return in_array( 'product', $button_locations, true ) || in_array( 'mini-cart', $button_locations, true );
}
return in_array( $this->context(), $button_locations, true );
}
/**
* Get context.
*
* @return string
*/
protected function context(): string {
$context = 'mini-cart';
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
$context = 'product';
}
if ( is_cart() ) {
$context = 'cart';
}
if ( is_checkout() && ! $this->is_paypal_continuation() ) {
$context = 'checkout';
}
if ( is_checkout_pay_page() ) {
$context = 'pay-now';
}
return $context;
}
/**
* Whether is PayPal continuation or not.
*
* @return bool
*/
protected function is_paypal_continuation(): bool {
$order = $this->session_handler->order();
if ( ! $order ) {
return false;
}
$source = $order->payment_source();
if ( $source && $source->card() ) {
return false; // Ignore for DCC.
}
if ( 'card' === $this->session_handler->funding_source() ) {
return false; // Ignore for card buttons.
}
return true;
}
}