Skip to content

Commit

Permalink
Disable save to account checkout if subscription in the cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Emili Castells Guasch authored and Emili Castells Guasch committed Jan 11, 2024
1 parent cd8cfaf commit 51aa79a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class CardFieldsRenderer {

show(buttonSelector);

if(this.defaultConfig.cart_contains_subscription) {
const saveToAccount = document.querySelector('#wc-ppcp-credit-card-gateway-new-payment-method');
if(saveToAccount) {
saveToAccount.checked = true;
saveToAccount.disabled = true;
}
}

document.querySelector(buttonSelector).addEventListener("click", (event) => {
event.preventDefault();
this.spinner.block();
Expand Down
1 change: 1 addition & 0 deletions modules/ppcp-button/src/Assets/SmartButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ public function script_data(): array {
'endpoint' => \WC_AJAX::get_endpoint( CartScriptParamsEndpoint::ENDPOINT ),
),
),
'cart_contains_subscription' => $this->subscription_helper->cart_contains_subscription(),
'subscription_plan_id' => $this->subscription_helper->paypal_subscription_id(),
'variable_paypal_subscription_variations' => $this->subscription_helper->variable_paypal_subscription_variations(),
'subscription_product_allowed' => $this->subscription_helper->checkout_subscription_product_allowed(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ document.addEventListener(
init()
});

if(ppcp_add_payment_method.is_subscription_change_payment_page) {
const saveToAccount = document.querySelector('#wc-ppcp-credit-card-gateway-new-payment-method');
if(saveToAccount) {
saveToAccount.checked = true;
saveToAccount.disabled = true;
}
}

setTimeout(() => {
loadScript({
clientId: ppcp_add_payment_method.client_id,
Expand Down
24 changes: 12 additions & 12 deletions modules/ppcp-wc-subscriptions/src/WcSubscriptionsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ function ( $default_fields, $id ) use ( $c ) {

add_filter(
'woocommerce_available_payment_gateways',
function( array $methods ): array {
if ( ! is_wc_endpoint_url( 'order-pay' ) ) {
function( array $methods ) use ( $c ) : array {
if (
! is_wc_endpoint_url( 'order-pay' )
|| $c->has( 'save-payment-methods.eligible' ) && $c->get( 'save-payment-methods.eligible' )
) {
return $methods;
}

Expand Down Expand Up @@ -261,18 +264,15 @@ protected function display_saved_paypal_payments(
&& $subscription_helper->is_subscription_change_payment()
) {
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id(), PayPalGateway::ID );
$output = sprintf(
'<p class="form-row form-row-wide"><label>%1$s</label><select id="saved-paypal-payment" name="saved_paypal_payment">',
esc_html__( 'Select PayPal payment', 'woocommerce-paypal-payments' )
);

$output = '<ul class="wc-saved-payment-methods">';
foreach ( $tokens as $token ) {
$output .= sprintf(
'<option value="%1$s">%2$s</option>',
$token->get_id(),
$token->get_meta( 'email' ) ?? ''
);
$output .= '<li>';
$output .= sprintf( '<input name="saved_paypal_payment" type="radio" value="%s" style="width:auto;" checked="checked">', $token->get_id() );
$output .= sprintf( '<label for="saved_paypal_payment">%s</label>', $token->get_meta( 'email' ) ?? '' );
$output .= '</li>';
}
$output .= '</select></p>';
$output .= '</ul>';

return $output;
}
Expand Down

0 comments on commit 51aa79a

Please sign in to comment.