Skip to content

Commit 933fd7a

Browse files
author
Anne Mirasol
committed
Enforce separate location settings for Apple Pay/Google Pay and Amazon Pay
1 parent 9a9355c commit 933fd7a

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

includes/class-wc-stripe-blocks-support.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public function __construct( $payment_request_configuration = null, $express_che
4646
$this->payment_request_configuration = null !== $payment_request_configuration ? $payment_request_configuration : new WC_Stripe_Payment_Request();
4747

4848
if ( null === $express_checkout_configuration ) {
49-
$helper = new WC_Stripe_Express_Checkout_Helper();
50-
$ajax_handler = new WC_Stripe_Express_Checkout_Ajax_Handler( $helper );
49+
$helper = new WC_Stripe_Express_Checkout_Helper();
50+
$ajax_handler = new WC_Stripe_Express_Checkout_Ajax_Handler( $helper );
5151
$express_checkout_configuration = new WC_Stripe_Express_Checkout_Element( $ajax_handler, $helper );
5252
}
5353
$this->express_checkout_configuration = $express_checkout_configuration;
@@ -283,30 +283,6 @@ private function should_show_express_checkout_button() {
283283
return false;
284284
}
285285

286-
// Don't show if ECEs are supposed to be hidden on the cart page.
287-
if (
288-
has_block( 'woocommerce/cart' )
289-
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_cart_page()
290-
) {
291-
return false;
292-
}
293-
294-
// Don't show if ECEs are supposed to be hidden on the checkout page.
295-
if (
296-
has_block( 'woocommerce/checkout' )
297-
&& ! $this->express_checkout_configuration->express_checkout_helper->should_show_ece_on_checkout_page()
298-
) {
299-
return false;
300-
}
301-
302-
// Don't show ECEs if there are unsupported products in the cart.
303-
if (
304-
( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) )
305-
&& ! $this->express_checkout_configuration->express_checkout_helper->allowed_items_in_cart()
306-
) {
307-
return false;
308-
}
309-
310286
return $this->express_checkout_configuration->express_checkout_helper->should_show_express_checkout_button();
311287
}
312288

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,24 +1523,42 @@ public function get_login_confirmation_settings() {
15231523
}
15241524

15251525
/**
1526-
* Pages where the express checkout buttons should be displayed.
1526+
* Check if the express checkout type is enabled for the given location.
15271527
*
1528-
* @return array
1528+
* @param string $express_checkout_type The type of express checkout.
1529+
* @param string $location The location to check.
1530+
* @return boolean
15291531
*/
1530-
public function get_button_locations() {
1531-
// If the locations have not been set return the default setting.
1532-
if ( ! isset( $this->stripe_settings['payment_request_button_locations'] ) ) {
1533-
return [ 'product', 'cart' ];
1532+
public function is_enabled_for_location( $express_checkout_type = 'payment_request', $location = '' ) {
1533+
switch ( $express_checkout_type ) {
1534+
case 'payment_request':
1535+
$key = 'payment_request_button_locations';
1536+
break;
1537+
case 'link':
1538+
// Link does not yet have its own Customize page.
1539+
$key = 'payment_request_button_locations';
1540+
break;
1541+
case 'amazon_pay':
1542+
$key = 'amazon_pay_button_locations';
1543+
break;
1544+
default:
1545+
$key = 'payment_request_button_locations';
1546+
break;
15341547
}
15351548

1536-
// If all locations are removed through the settings UI the location config will be set to
1537-
// an empty string "". If that's the case (and if the settings are not an array for any
1538-
// other reason) we should return an empty array.
1539-
if ( ! is_array( $this->stripe_settings['payment_request_button_locations'] ) ) {
1540-
return [];
1549+
if ( ! isset( $this->stripe_settings[ $key ] ) ) {
1550+
// If the locations have not been set/modified, return the default setting.
1551+
$enabled_locations = [ 'product', 'cart' ];
1552+
} elseif ( ! is_array( $this->stripe_settings[ $key ] ) ) {
1553+
// If all locations are removed through the settings UI the location config will be set to
1554+
// an empty string "". If that's the case (and if the settings are not an array for any
1555+
// other reason) we should return an empty array.
1556+
$enabled_locations = [];
1557+
} else {
1558+
$enabled_locations = $this->stripe_settings[ $key ];
15411559
}
15421560

1543-
return $this->stripe_settings['payment_request_button_locations'];
1561+
return in_array( $location, $enabled_locations, true );
15441562
}
15451563

15461564
/**
@@ -1560,7 +1578,14 @@ public function is_express_checkout_enabled() {
15601578
* @return boolean
15611579
*/
15621580
public function is_payment_request_enabled() {
1563-
return $this->gateway->is_payment_request_enabled();
1581+
$is_enabled = $this->gateway->is_payment_request_enabled();
1582+
if ( is_product() ) {
1583+
return $is_enabled && $this->is_enabled_for_location( 'payment_request', 'product' );
1584+
} elseif ( is_cart() ) {
1585+
return $is_enabled && $this->is_enabled_for_location( 'payment_request', 'cart' );
1586+
}
1587+
1588+
return $is_enabled;
15641589
}
15651590

15661591
/**
@@ -1569,7 +1594,14 @@ public function is_payment_request_enabled() {
15691594
* @return boolean
15701595
*/
15711596
public function is_amazon_pay_enabled() {
1572-
return WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled( $this->gateway );
1597+
$is_enabled = WC_Stripe_UPE_Payment_Method_Amazon_Pay::is_amazon_pay_enabled( $this->gateway );
1598+
if ( is_product() ) {
1599+
return $is_enabled && $this->is_enabled_for_location( 'amazon_pay', 'product' );
1600+
} elseif ( is_cart() ) {
1601+
return $is_enabled && $this->is_enabled_for_location( 'amazon_pay', 'cart' );
1602+
}
1603+
1604+
return $is_enabled;
15731605
}
15741606

15751607
/**

0 commit comments

Comments
 (0)