Skip to content

Commit f980d7c

Browse files
superdav42claude
andcommitted
Hide redundant billing address fields when Stripe gateway is selected
When the billing address field is configured to show only ZIP and Country (zip_and_country mode), these fields are now automatically hidden when any Stripe gateway is active. Both Stripe Payment Element and Stripe Checkout collect Country and ZIP in their own UI, making separate fields redundant. Changes: - Add :style binding to billing address field wrappers that hides them when gateway starts with 'stripe' - Use Vue :style binding instead of v-show for better compatibility with server-rendered in-DOM templates - Add sync_billing_address_to_stripe() method to pre-fill billing address in Stripe Checkout modal 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent b0155af commit f980d7c

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

inc/checkout/signup-fields/class-signup-field-billing-address.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,25 @@ public function to_fields_array($attributes) {
268268

269269
foreach ($fields as &$field) {
270270
$field['wrapper_classes'] = trim(wu_get_isset($field, 'wrapper_classes', '') . ' ' . $attributes['element_classes']);
271+
272+
/*
273+
* When zip_and_country is enabled (showing only ZIP + country),
274+
* hide the billing address fields when any Stripe gateway is selected.
275+
* Both Stripe Payment Element and Stripe Checkout collect Country and ZIP,
276+
* making these fields redundant.
277+
*
278+
* Using :style binding instead of v-show for better Vue compatibility
279+
* with server-rendered in-DOM templates.
280+
*/
281+
if ($zip_only) {
282+
// Ensure wrapper_html_attr array exists
283+
if ( ! isset($field['wrapper_html_attr'])) {
284+
$field['wrapper_html_attr'] = [];
285+
}
286+
287+
// Use :style binding to hide element when any Stripe gateway is selected
288+
$field['wrapper_html_attr'][':style'] = "{ display: gateway && gateway.startsWith('stripe') ? 'none' : '' }";
289+
}
271290
}
272291

273292
uasort($fields, 'wu_sort_by_order');

inc/gateways/class-stripe-checkout-gateway.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ public function run_preflight() {
221221
*/
222222
$s_customer = $this->get_or_create_customer($this->customer->get_id());
223223

224+
/*
225+
* Update the Stripe customer with the current billing address.
226+
* This ensures the address is pre-filled in Stripe Checkout.
227+
*/
228+
$this->sync_billing_address_to_stripe($s_customer->id);
229+
224230
/*
225231
* Stripe Checkout allows for tons of different payment methods.
226232
* These include:
@@ -493,4 +499,61 @@ public function get_user_saved_payment_methods() {
493499
return [];
494500
}
495501
}
502+
503+
/**
504+
* Syncs the customer's billing address to the Stripe customer object.
505+
*
506+
* This ensures that Stripe Checkout has the latest billing address
507+
* pre-filled when the checkout modal opens.
508+
*
509+
* @since 2.3.0
510+
*
511+
* @param string $stripe_customer_id The Stripe customer ID.
512+
* @return void
513+
*/
514+
protected function sync_billing_address_to_stripe(string $stripe_customer_id): void {
515+
516+
$billing_address = $this->customer->get_billing_address();
517+
518+
/*
519+
* Only update if we have billing address data.
520+
*/
521+
if (empty($billing_address->billing_country) && empty($billing_address->billing_zip_code)) {
522+
return;
523+
}
524+
525+
try {
526+
$stripe_address = $this->convert_to_stripe_address($billing_address);
527+
528+
$update_data = [
529+
'address' => $stripe_address,
530+
];
531+
532+
/*
533+
* Also update name and email if available.
534+
*/
535+
if ($this->customer->get_display_name()) {
536+
$update_data['name'] = $this->customer->get_display_name();
537+
}
538+
539+
if ($this->customer->get_email_address()) {
540+
$update_data['email'] = $this->customer->get_email_address();
541+
}
542+
543+
$this->get_stripe_client()->customers->update($stripe_customer_id, $update_data);
544+
} catch (\Throwable $exception) {
545+
/*
546+
* Log the error but don't fail the checkout.
547+
* Stripe Checkout will still collect the address.
548+
*/
549+
wu_log_add(
550+
'stripe-checkout',
551+
sprintf(
552+
'Failed to sync billing address to Stripe customer %s: %s',
553+
$stripe_customer_id,
554+
$exception->getMessage()
555+
)
556+
);
557+
}
558+
}
496559
}

0 commit comments

Comments
 (0)