Skip to content

Commit a18ccb6

Browse files
committed
Remove Amazon Pay from payment gateways when changing subscription payment method if customer is logged out of Amazon account
Change redirect url if request redirect is empty.
1 parent 8fe8748 commit a18ccb6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

woocommerce-gateway-amazon-payments-advanced.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public function __construct() {
186186
add_action( 'wp_ajax_amazon_get_order_reference', array( $this, 'ajax_get_order_reference' ) );
187187
add_action( 'wp_ajax_nopriv_amazon_get_order_reference', array( $this, 'ajax_get_order_reference' ) );
188188

189+
// WC Subscription Hook
190+
add_filter( 'woocommerce_subscriptions_process_payment_for_change_method_via_pay_shortcode', array( $this, 'filter_payment_method_changed_result' ), 10, 2 );
189191
}
190192

191193
/**
@@ -501,6 +503,7 @@ public function checkout_init( $checkout ) {
501503
// Don't try to render the Amazon widgets if we don't have the prerequisites
502504
// for each mode.
503505
if ( ( ! $enable_login_app && empty( $this->reference_id ) ) || ( $enable_login_app && empty( $this->access_token ) ) ) {
506+
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'remove_amazon_gateway' ) );
504507
return;
505508
}
506509

@@ -1104,6 +1107,24 @@ public function update_amazon_widgets_fragment( $fragments ) {
11041107

11051108
}
11061109

1110+
/**
1111+
* Remove amazon gateway.
1112+
*
1113+
* @param $gateways
1114+
*
1115+
* @return array
1116+
*/
1117+
public function remove_amazon_gateway( $gateways ) {
1118+
1119+
foreach ( $gateways as $gateway_key => $gateway ) {
1120+
if ( 'amazon_payments_advanced' === $gateway_key ) {
1121+
unset( $gateways[ $gateway_key ] );
1122+
}
1123+
}
1124+
1125+
return $gateways;
1126+
}
1127+
11071128
/**
11081129
* Remove all gateways except amazon
11091130
*
@@ -1639,6 +1660,21 @@ protected function amazon_keys_already_set() {
16391660
private function get_settings_url() {
16401661
return admin_url( 'admin.php?page=wc-settings&tab=checkout&section=amazon_payments_advanced' );
16411662
}
1663+
1664+
/**
1665+
* Set redirect URL if the result redirect URL is empty
1666+
*
1667+
* @param mixed $result
1668+
* @param WC_Subscription $subscription
1669+
*
1670+
* @return mixed
1671+
*/
1672+
public function filter_payment_method_changed_result( $result, $subscription ) {
1673+
if ( empty( $result['redirect'] ) && ! empty( $subscription ) && method_exists( $subscription, 'get_view_order_url' ) ) {
1674+
$result['redirect'] = $subscription->get_view_order_url();
1675+
}
1676+
return $result;
1677+
}
16421678
}
16431679

16441680
/**

0 commit comments

Comments
 (0)