Refactor smart button with new settings

This commit is contained in:
Narek Zakarian 2022-12-05 16:53:30 +04:00
parent 9c6a813776
commit da07c3b5fa
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
2 changed files with 36 additions and 49 deletions

View file

@ -406,11 +406,9 @@ class SmartButton implements SmartButtonInterface {
*/
private function render_button_wrapper_registrar(): bool {
$not_enabled_on_product_page = $this->settings->has( 'button_single_product_enabled' ) &&
! $this->settings->get( 'button_single_product_enabled' );
if (
( is_product() || wc_post_content_has_shortcode( 'product_page' ) )
&& ! $not_enabled_on_product_page
&& $this->settings_status->is_smart_button_enabled_for_location( 'product' )
// TODO: it seems like there is no easy way to properly handle vaulted PayPal free trial,
// so disable the buttons for now everywhere except checkout for free trial.
&& ! $this->is_free_trial_product()
@ -435,10 +433,8 @@ class SmartButton implements SmartButtonInterface {
);
}
$not_enabled_on_minicart = $this->settings->has( 'button_mini_cart_enabled' ) &&
! $this->settings->get( 'button_mini_cart_enabled' );
if (
! $not_enabled_on_minicart
$this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' )
&& ! $this->is_free_trial_cart()
) {
add_action(
@ -479,12 +475,11 @@ class SmartButton implements SmartButtonInterface {
}
);
$not_enabled_on_cart = $this->settings->has( 'button_cart_enabled' ) &&
! $this->settings->get( 'button_cart_enabled' );
$enabled_on_cart = $this->settings_status->is_smart_button_enabled_for_location( 'cart' );
add_action(
$this->proceed_to_checkout_button_renderer_hook(),
function() use ( $not_enabled_on_cart ) {
if ( ! is_cart() || $not_enabled_on_cart || $this->is_free_trial_cart() || $this->is_cart_price_total_zero() ) {
function() use ( $enabled_on_cart ) {
if ( ! is_cart() || ! $enabled_on_cart || $this->is_free_trial_cart() || $this->is_cart_price_total_zero() ) {
return;
}
@ -868,8 +863,8 @@ class SmartButton implements SmartButtonInterface {
'shipping_field' => _x( 'Shipping %s', 'checkout-validation', 'woocommerce' ),
),
'order_id' => 'pay-now' === $this->context() ? absint( $wp->query_vars['order-pay'] ) : 0,
'single_product_buttons_enabled' => $this->settings->has( 'button_product_enabled' ) && $this->settings->get( 'button_product_enabled' ),
'mini_cart_buttons_enabled' => $this->settings->has( 'button_mini-cart_enabled' ) && $this->settings->get( 'button_mini-cart_enabled' ),
'single_product_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'product' ),
'mini_cart_buttons_enabled' => $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' ),
'basic_checkout_validation_enabled' => $this->basic_checkout_validation_enabled,
);
@ -1050,45 +1045,21 @@ class SmartButton implements SmartButtonInterface {
* @throws NotFoundException If a setting has not been found.
*/
private function load_button_component() : bool {
$load_buttons = false;
if (
$this->context() === 'checkout'
&& $this->settings->has( 'button_enabled' )
&& $this->settings->get( 'button_enabled' )
) {
$load_buttons = true;
}
if (
$this->context() === 'product'
&& (
( $this->settings->has( 'button_product_enabled' ) && $this->settings->get( 'button_product_enabled' ) ) ||
( $this->settings_status->is_pay_later_messaging_enabled_for_location( 'product' ) )
)
) {
$load_buttons = true;
}
if (
$this->settings->has( 'button_mini-cart_enabled' )
&& $this->settings->get( 'button_mini-cart_enabled' )
) {
$load_buttons = true;
}
$smart_button_enabled_for_current_location = $this->settings_status->is_smart_button_enabled_for_location( $this->context() );
$smart_button_enabled_for_mini_cart = $this->settings_status->is_smart_button_enabled_for_location( 'mini-cart' );
$messaging_enabled_for_current_location = $this->settings_status->is_pay_later_messaging_enabled_for_location( $this->context() );
if (
$this->context() === 'cart'
&& (
( $this->settings->has( 'button_cart_enabled' ) && $this->settings->get( 'button_cart_enabled' ) ) ||
( $this->settings_status->is_pay_later_messaging_enabled_for_location( 'cart' ) )
)
) {
$load_buttons = true;
switch ( $this->context() ) {
case 'checkout':
case 'cart':
return $smart_button_enabled_for_current_location || $messaging_enabled_for_current_location;
case 'product':
return $smart_button_enabled_for_current_location || $messaging_enabled_for_current_location || $smart_button_enabled_for_mini_cart;
case 'pay-now':
return true;
default:
return $smart_button_enabled_for_mini_cart;
}
if ( $this->context() === 'pay-now' ) {
$load_buttons = true;
}
return $load_buttons;
}
/**

View file

@ -125,4 +125,20 @@ class SettingsStatus {
return $context === 'product' ? $enabled_for_product || $enabled_for_mini_cart : $enabled_for_current_location;
}
/**
* Checks whether smart buttons are enabled for a given location.
*
* @param string $location The location.
* @return bool true if is enabled, otherwise false.
*/
public function is_smart_button_enabled_for_location( string $location ): bool {
$selected_locations = $this->settings->has( 'smart_button_locations' ) ? $this->settings->get( 'smart_button_locations' ) : array();
if ( empty( $selected_locations ) ) {
return false;
}
return in_array( $location, $selected_locations, true );
}
}