Refactor Pay Later to read from new tab setting on frontend

This commit is contained in:
Narek Zakarian 2022-10-31 15:59:17 +04:00
parent c68eac814f
commit 832f7a41cd
6 changed files with 170 additions and 114 deletions

View file

@ -296,6 +296,9 @@ class PayPalGateway extends \WC_Payment_Gateway {
if ( $this->is_credit_card_tab() ) {
return __( 'Advanced Card Processing', 'woocommerce-paypal-payments' );
}
if ( $this->is_pay_later_tab() ) {
return __( 'Pay Later Button', 'woocommerce-paypal-payments' );
}
if ( $this->is_paypal_tab() ) {
return __( 'Standard Payments', 'woocommerce-paypal-payments' );
}
@ -323,6 +326,13 @@ class PayPalGateway extends \WC_Payment_Gateway {
);
}
if ( $this->is_pay_later_tab() ) {
return __(
'Example description.',
'woocommerce-paypal-payments'
);
}
if ( is_admin() ) {
return __(
'Accept PayPal, Pay Later and alternative payment types.',
@ -372,6 +382,16 @@ class PayPalGateway extends \WC_Payment_Gateway {
&& Settings::CONNECTION_TAB_ID === $this->page_id;
}
/**
* Whether we are on the pay-later tab.
*
* @return bool true if is pay-later tab, otherwise false
*/
protected function is_pay_later_tab() : bool {
return is_admin()
&& Settings::PAY_LATER_TAB_ID === $this->page_id;
}
/**
* Whether we are on the PayPal settings tab.
*

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Helper;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
@ -35,21 +36,93 @@ class SettingsStatus {
/**
* Check whether Pay Later message is enabled either for checkout, cart or product page.
*
* @return bool
* @throws \WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException When a setting was not found.
* @return bool true if is enabled, otherwise false.
* @throws NotFoundException When a setting was not found.
*/
public function pay_later_messaging_is_enabled(): bool {
$pay_later_message_enabled_for_checkout = $this->settings->has( 'message_enabled' )
&& (bool) $this->settings->get( 'message_enabled' );
$messaging_enabled = $this->settings->has( 'pay_later_messaging_enabled' ) && $this->settings->get( 'pay_later_messaging_enabled' );
$selected_locations = $this->settings->has( 'pay_later_messaging_locations' ) ? $this->settings->get( 'pay_later_messaging_locations' ) : array();
$pay_later_message_enabled_for_cart = $this->settings->has( 'message_cart_enabled' )
&& (bool) $this->settings->get( 'message_cart_enabled' );
return $messaging_enabled && ! empty( $selected_locations );
}
$pay_later_message_enabled_for_product = $this->settings->has( 'message_product_enabled' )
&& (bool) $this->settings->get( 'message_product_enabled' );
/**
* Check whether Pay Later message is enabled for a given location.
*
* @param string $location The location setting name.
* @return bool true if is enabled, otherwise false.
* @throws NotFoundException When a setting was not found.
*/
public function pay_later_messaging_is_enabled_for_location( string $location ): bool {
if ( ! $this->pay_later_messaging_is_enabled() ) {
return false;
}
return $pay_later_message_enabled_for_checkout ||
$pay_later_message_enabled_for_cart ||
$pay_later_message_enabled_for_product;
$selected_locations = $this->settings->has( 'pay_later_messaging_locations' ) ? $this->settings->get( 'pay_later_messaging_locations' ) : array();
if ( empty( $selected_locations ) ) {
return false;
}
return in_array( $location, $selected_locations, true );
}
/**
* Check whether Pay Later button is enabled either for checkout, cart or product page.
*
* @return bool true if is enabled, otherwise false.
* @throws NotFoundException When a setting was not found.
*/
public function pay_later_button_is_enabled(): bool {
$messaging_enabled = $this->settings->has( 'pay_later_button_enabled' ) && $this->settings->get( 'pay_later_button_enabled' );
$selected_locations = $this->settings->has( 'pay_later_button_locations' ) ? $this->settings->get( 'pay_later_button_locations' ) : array();
return $messaging_enabled && ! empty( $selected_locations );
}
/**
* Check whether Pay Later button is enabled for a given location.
*
* @param string $location The location.
* @return bool true if is enabled, otherwise false.
* @throws NotFoundException When a setting was not found.
*/
public function pay_later_button_is_enabled_for_location( string $location ): bool {
if ( ! $this->pay_later_button_is_enabled() ) {
return false;
}
$selected_locations = $this->settings->has( 'pay_later_button_locations' ) ? $this->settings->get( 'pay_later_button_locations' ) : array();
if ( empty( $selected_locations ) ) {
return false;
}
return in_array( $location, $selected_locations, true );
}
/**
* Check whether Pay Later button is enabled for a given context.
*
* @param string $context The context.
* @return bool true if is enabled, otherwise false.
* @throws NotFoundException When a setting was not found.
*/
public function pay_later_button_is_enabled_for_context( string $context ): bool {
if ( ! $this->pay_later_button_is_enabled() ) {
return false;
}
$selected_locations = $this->settings->has( 'pay_later_button_locations' ) ? $this->settings->get( 'pay_later_button_locations' ) : array();
if ( empty( $selected_locations ) ) {
return false;
}
$enabled_for_current_location = $this->pay_later_button_is_enabled_for_location( $context );
$enabled_for_product = $this->pay_later_button_is_enabled_for_location( 'product' );
$enabled_for_mini_cart = $this->pay_later_button_is_enabled_for_location( 'mini-cart' );
return $context === 'product' ? $enabled_for_product || $enabled_for_mini_cart : $enabled_for_current_location;
}
}

View file

@ -34,6 +34,7 @@ trait PageMatcherTrait {
$gateway_page_id_map = array(
Settings::CONNECTION_TAB_ID => Settings::CONNECTION_TAB_ID,
PayPalGateway::ID => 'paypal',
Settings::PAY_LATER_TAB_ID => Settings::PAY_LATER_TAB_ID,
CreditCardGateway::ID => 'dcc', // TODO: consider using just the gateway ID for PayPal and DCC too.
CardButtonGateway::ID => CardButtonGateway::ID,
);

View file

@ -76,7 +76,7 @@ class SectionsRenderer {
foreach ( $this->sections as $id => $label ) {
$url = admin_url( 'admin.php?page=wc-settings&tab=checkout&section=' . $id );
if ( in_array( $id, array( Settings::CONNECTION_TAB_ID, CreditCardGateway::ID ), true ) ) {
if ( in_array( $id, array( Settings::CONNECTION_TAB_ID, CreditCardGateway::ID, Settings::PAY_LATER_TAB_ID ), true ) ) {
// We need section=ppcp-gateway for the webhooks page because it is not a gateway,
// and for DCC because otherwise it will not render the page if gateway is not available (country/currency).
// Other gateways render fields differently, and their pages are not expected to work when gateway is not available.

View file

@ -246,10 +246,17 @@ class SettingsListener {
return;
}
$this->settings->set( 'message_enabled', false );
$this->settings->set( 'message_product_enabled', false );
$this->settings->set( 'message_cart_enabled', false );
$this->settings->persist();
$pay_later_messaging_enabled = $this->settings->has( 'pay_later_messaging_enabled' ) && $this->settings->get( 'pay_later_messaging_enabled' );
if ( $pay_later_messaging_enabled ) {
$this->settings->set( 'pay_later_messaging_enabled', false );
$this->settings->persist();
}
$pay_later_button_enabled = $this->settings->has( 'pay_later_button_enabled' ) && $this->settings->get( 'pay_later_button_enabled' );
if ( $pay_later_button_enabled ) {
$this->settings->set( 'pay_later_button_enabled', false );
$this->settings->persist();
}
}
/**