Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ExpressCheckoutPreviewComponent = ( { size } ) => {
link: 'never',
googlePay: 'never',
applePay: 'never',
klarna: 'never',
},
layout: { overflow: 'never' },
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const ExpressCheckoutPreviewComponent = ( { buttonType, theme, size } ) => {
link: 'never',
googlePay: 'always',
applePay: 'always',
amazonPay: 'never',
klarna: 'never',
},
layout: { overflow: 'never' },
};
Expand Down
33 changes: 2 additions & 31 deletions includes/class-wc-stripe-blocks-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function __construct( $payment_request_configuration = null, $express_che
$this->payment_request_configuration = null !== $payment_request_configuration ? $payment_request_configuration : new WC_Stripe_Payment_Request();

if ( null === $express_checkout_configuration ) {
$helper = new WC_Stripe_Express_Checkout_Helper();
$ajax_handler = new WC_Stripe_Express_Checkout_Ajax_Handler( $helper );
$helper = new WC_Stripe_Express_Checkout_Helper();
$ajax_handler = new WC_Stripe_Express_Checkout_Ajax_Handler( $helper );
$express_checkout_configuration = new WC_Stripe_Express_Checkout_Element( $ajax_handler, $helper );
}
$this->express_checkout_configuration = $express_checkout_configuration;
Expand Down Expand Up @@ -278,35 +278,6 @@ private function get_style() {
* @return boolean True if ECEs should be displayed, false otherwise.
*/
private function should_show_express_checkout_button() {
// Don't show if ECEs are turned off in settings.
if ( ! $this->express_checkout_configuration->express_checkout_helper->is_express_checkout_enabled() ) {
return false;
}

// Don't show if ECEs are supposed to be hidden on the cart page.
if (
has_block( 'woocommerce/cart' )
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page()
) {
return false;
}

// Don't show if ECEs are supposed to be hidden on the checkout page.
if (
has_block( 'woocommerce/checkout' )
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_checkout_page()
) {
return false;
}

// Don't show ECEs if there are unsupported products in the cart.
if (
( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) )
&& ! $this->express_checkout_configuration->express_checkout_helper->allowed_items_in_cart()
) {
return false;
}

return $this->express_checkout_configuration->express_checkout_helper->should_show_express_checkout_button();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public function display_express_checkout_button_separator_html() {
return;
}

if ( is_checkout() && ! in_array( 'checkout', $this->express_checkout_helper->get_button_locations(), true ) ) {
if ( is_checkout() && ! $this->express_checkout_helper->should_show_on_checkout_page() ) {
return;
}

Expand Down
116 changes: 94 additions & 22 deletions includes/payment-methods/class-wc-stripe-express-checkout-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,13 @@ public function should_show_express_checkout_button() {
}

// Don't show on cart if disabled.
if ( is_cart() && ! $this->should_show_ece_on_cart_page() ) {
if ( $this->is_cart() && ! $this->should_show_ece_on_cart_page() ) {
WC_Stripe_Logger::log( 'Stripe Express Checkout buttons display on cart is disabled. ' );
return false;
}

// Don't show on checkout if disabled.
if ( is_checkout() && ! $this->should_show_ece_on_checkout_page() ) {
if ( $this->is_checkout() && ! $this->should_show_ece_on_checkout_page() ) {
WC_Stripe_Logger::log( 'Stripe Express Checkout buttons display on checkout is disabled. ' );
return false;
}
Expand Down Expand Up @@ -818,13 +818,13 @@ private function product_or_cart_needs_shipping() {
}

/**
* Returns true if express checkout buttons are enabled on the cart page, false
* Returns true if any express checkout buttons are enabled on the cart page, false
* otherwise.
*
* @return boolean True if express checkout buttons are enabled on the cart page, false otherwise
* @return boolean True if any express checkout buttons are enabled on the cart page, false otherwise.
*/
public function should_show_ece_on_cart_page() {
$should_show_on_cart_page = in_array( 'cart', $this->get_button_locations(), true );
$should_show_on_cart_page = $this->should_show_ece_on_page( 'cart' );

return apply_filters(
'wc_stripe_show_payment_request_on_cart',
Expand All @@ -841,7 +841,7 @@ public function should_show_ece_on_cart_page() {
public function should_show_ece_on_checkout_page() {
global $post;

$should_show_on_checkout_page = in_array( 'checkout', $this->get_button_locations(), true );
$should_show_on_checkout_page = $this->should_show_ece_on_page( 'checkout' );

return apply_filters(
'wc_stripe_show_payment_request_on_checkout',
Expand All @@ -851,15 +851,15 @@ public function should_show_ece_on_checkout_page() {
}

/**
* Returns true if express checkout buttons are enabled on product pages, false
* Returns true if any express checkout buttons are enabled on product pages, false
* otherwise.
*
* @return boolean True if express checkout buttons are enabled on product pages, false otherwise
* @return boolean True if any express checkout buttons are enabled on product pages, false otherwise
*/
public function should_show_ece_on_product_pages() {
global $post;

$should_show_on_product_page = in_array( 'product', $this->get_button_locations(), true );
$should_show_on_product_page = $this->should_show_ece_on_page( 'product' );

// Note the negation because if the filter returns `true` that means we should hide the PRB.
return ! apply_filters(
Expand All @@ -869,6 +869,18 @@ public function should_show_ece_on_product_pages() {
);
}

/**
* Returns true if any express checkout buttons are enabled on the given page, false
* otherwise.
*
* @param string $page The page to check.
* @return boolean True if any express checkout buttons are enabled on the given page, false otherwise.
*/
private function should_show_ece_on_page( $page ) {
return $this->is_enabled_for_location( 'payment_request', $page ) ||
$this->is_enabled_for_location( 'amazon_pay', $page );
}

/**
* Returns true if the provided product is supported, false otherwise.
*
Expand Down Expand Up @@ -1354,6 +1366,15 @@ public function is_checkout() {
return is_checkout() || has_block( 'woocommerce/checkout' );
}

/**
* Checks if this is the cart page or content contains a cart block.
*
* @return boolean
*/
public function is_cart() {
return is_cart() || has_block( 'woocommerce/cart' );
}

/**
* Builds the shippings methods to pass to express checkout elements.
*/
Expand Down Expand Up @@ -1525,22 +1546,52 @@ public function get_login_confirmation_settings() {
/**
* Pages where the express checkout buttons should be displayed.
*
* @param string $express_checkout_type The type of express checkout.
* @return array
*/
public function get_button_locations() {
// If the locations have not been set return the default setting.
if ( ! isset( $this->stripe_settings['payment_request_button_locations'] ) ) {
return [ 'product', 'cart' ];
public function get_button_locations( $express_checkout_type ) {
switch ( $express_checkout_type ) {
case 'payment_request':
$key = 'payment_request_button_locations';
break;
case 'link':
// Link does not yet have its own Customize page. It shares the same location settings
// as Apple Pay and Google Pay.
$key = 'payment_request_button_locations';
break;
case 'amazon_pay':
$key = 'amazon_pay_button_locations';
break;
default:
$key = 'payment_request_button_locations';
break;
}

// If all locations are removed through the settings UI the location config will be set to
// an empty string "". If that's the case (and if the settings are not an array for any
// other reason) we should return an empty array.
if ( ! is_array( $this->stripe_settings['payment_request_button_locations'] ) ) {
return [];
if ( ! isset( $this->stripe_settings[ $key ] ) ) {
// If the locations have not been set/modified, return the default setting.
$enabled_locations = [ 'product', 'cart' ];
} elseif ( ! is_array( $this->stripe_settings[ $key ] ) ) {
// If all locations are removed through the settings UI the location config will be set to
// an empty string "". If that's the case (and if the settings are not an array for any
// other reason) we should return an empty array.
$enabled_locations = [];
} else {
$enabled_locations = $this->stripe_settings[ $key ];
}

return $this->stripe_settings['payment_request_button_locations'];
return $enabled_locations;
}

/**
* Check if the express checkout type is enabled for the given location.
*
* @param string $express_checkout_type The type of express checkout.
* @param string $location The location to check.
* @return boolean
*/
public function is_enabled_for_location( $express_checkout_type = 'payment_request', $location = '' ) {
$enabled_locations = $this->get_button_locations( $express_checkout_type );
return in_array( $location, $enabled_locations, true );
}

/**
Expand All @@ -1560,7 +1611,14 @@ public function is_express_checkout_enabled() {
* @return boolean
*/
public function is_payment_request_enabled() {
return $this->gateway->is_payment_request_enabled();
$is_enabled = $this->gateway->is_payment_request_enabled();
if ( is_product() ) {
return $is_enabled && $this->is_enabled_for_location( 'payment_request', 'product' );
} elseif ( is_cart() ) {
return $is_enabled && $this->is_enabled_for_location( 'payment_request', 'cart' );
}

return $is_enabled;
}

/**
Expand All @@ -1569,7 +1627,14 @@ public function is_payment_request_enabled() {
* @return boolean
*/
public function is_amazon_pay_enabled() {
return WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled( $this->gateway );
$is_enabled = WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled( $this->gateway );
if ( is_product() ) {
return $is_enabled && $this->is_enabled_for_location( 'amazon_pay', 'product' );
} elseif ( is_cart() ) {
return $is_enabled && $this->is_enabled_for_location( 'amazon_pay', 'cart' );
}

return $is_enabled;
}

/**
Expand All @@ -1578,7 +1643,14 @@ public function is_amazon_pay_enabled() {
* @return boolean
*/
public function is_link_enabled() {
return WC_Stripe_UPE_Payment_Method_Link::is_link_enabled( $this->gateway );
$is_enabled = WC_Stripe_UPE_Payment_Method_Link::is_link_enabled( $this->gateway );
if ( is_product() ) {
return $is_enabled && $this->is_enabled_for_location( 'link', 'product' );
} elseif ( is_cart() ) {
return $is_enabled && $this->is_enabled_for_location( 'link', 'cart' );
}

return $is_enabled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,6 @@ function () use ( $is_order_pay ) {
->disableOriginalConstructor()
->getMock();

$helper = $this->getMockBuilder( WC_Stripe_Express_Checkout_Helper::class )
->disableOriginalConstructor()
->setMethods( [ 'get_button_locations' ] )
->getMock();

$helper->expects( $this->any() )
->method( 'get_button_locations' )
->willReturn( [ $button_location ] );

$element = new WC_Stripe_Express_Checkout_Element( $ajax_handler, $helper );

ob_start();
Expand Down
Loading