Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modal-checkout): ensure customer location when state is required but country is not #1683

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static function init() {
add_filter( 'wcs_place_subscription_order_text', [ __CLASS__, 'order_button_text' ], 1 );
add_filter( 'woocommerce_order_button_text', [ __CLASS__, 'order_button_text' ] );
add_filter( 'option_woocommerce_subscriptions_order_button_text', [ __CLASS__, 'order_button_text' ] );
add_action( 'option_woocommerce_default_customer_address', [ __CLASS__, 'ensure_base_default_customer_address' ] );
add_action( 'default_option_woocommerce_default_customer_address', [ __CLASS__, 'ensure_base_default_customer_address' ] );

/** Custom handling for registered users. */
add_filter( 'woocommerce_checkout_customer_id', [ __CLASS__, 'associate_existing_user' ] );
Expand Down Expand Up @@ -996,6 +998,46 @@ public static function order_button_text( $text ) {
return $text;
}

/**
* Force option for base country for new customers if unset and billing country optional while state is required
* unless the NEWSPACK_PREVENT_FORCE_BASE_DEFAULT_CUSTOMER_ADDRESS constant is set.
*
* If this option is empty AND billing state is set as a required field AND billing country is not,
* validation of the state value will fail during modal checkout.
*
* See Default Customer Location in: https://woo.com/document/configuring-woocommerce-settings/#general-options
*
* @param string $option_value The value of the default customer address option.
*
* @return string Option value.
*/
public static function ensure_base_default_customer_address( $option_value ) {
// If the option is set, we're good.
if ( ! empty( $option_value ) ) {
return $option_value;
}

// Only in modal checkout.
if ( ! self::is_modal_checkout() ) {
return $option_value;
}

// Escape hatch in case we want the standard behavior even in modal checkout.
if ( defined( 'NEWSPACK_PREVENT_FORCE_BASE_DEFAULT_CUSTOMER_ADDRESS' ) && NEWSPACK_PREVENT_FORCE_BASE_DEFAULT_CUSTOMER_ADDRESS ) {
return $option_value;
}

// If billing state is required but billing country is not, we need to ensure a default location is set.
if ( defined( '\Newspack\Donations::DONATION_BILLING_FIELDS_OPTION' ) ) {
$billing_fields = get_option( \Newspack\Donations::DONATION_BILLING_FIELDS_OPTION, [] );
if ( ! in_array( 'billing_country', $billing_fields, true ) && in_array( 'billing_state', $billing_fields, true ) ) {
return 'base';
}
}

return $option_value;
}

/**
* If a reader tries to make a purchase with an email address that
* has been previously registered, automatically associate the transaction
Expand Down