Allow create payment when changing subscription payment method (WIP)

This commit is contained in:
Emili Castells Guasch 2024-01-08 12:40:56 +01:00
parent 16eebecc5a
commit dc9ad87b3e
3 changed files with 59 additions and 43 deletions

View file

@ -212,6 +212,19 @@ trait ContextTrait {
return $page_id && is_page( $page_id ) && isset( $wp->query_vars['add-payment-method'] ); return $page_id && is_page( $page_id ) && isset( $wp->query_vars['add-payment-method'] );
} }
/**
* Checks whether this user is changing the payment method for a subscription.
*
* @return bool
*/
private function is_subscription_change_payment_method_page(): bool {
if ( isset( $_GET['change_payment_method'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
return wcs_is_subscription( wc_clean( wp_unslash( $_GET['change_payment_method'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
return false;
}
/** /**
* Checks if it is the block editor page. * Checks if it is the block editor page.
*/ */

View file

@ -38,6 +38,8 @@ document.addEventListener(
.then((paypal) => { .then((paypal) => {
errorHandler.clear(); errorHandler.clear();
const paypalButtonContainer = document.querySelector(`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`);
if(paypalButtonContainer) {
paypal.Buttons( paypal.Buttons(
{ {
createVaultSetupToken: async () => { createVaultSetupToken: async () => {
@ -86,6 +88,7 @@ document.addEventListener(
} }
}, },
).render(`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`); ).render(`#ppc-button-${PaymentMethods.PAYPAL}-save-payment-method`);
}
const cardField = paypal.CardFields({ const cardField = paypal.CardFields({
createVaultSetupToken: async () => { createVaultSetupToken: async () => {
@ -167,7 +170,7 @@ document.addEventListener(
} }
} }
document.querySelector('#place_order').addEventListener("click", (event) => { document.querySelector('#place_order')?.addEventListener("click", (event) => {
event.preventDefault(); event.preventDefault();
cardField.submit() cardField.submit()

View file

@ -176,7 +176,7 @@ class SavePaymentMethodsModule implements ModuleInterface {
add_action( add_action(
'wp_enqueue_scripts', 'wp_enqueue_scripts',
function() use ( $c ) { function() use ( $c ) {
if ( ! is_user_logged_in() || ! $this->is_add_payment_method_page() ) { if ( ! is_user_logged_in() || ! ( $this->is_add_payment_method_page() || $this->is_subscription_change_payment_method_page() ) ) {
return; return;
} }