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

Allow to specify if billing address is required for orders #3658

Merged
merged 3 commits into from
Jun 22, 2020

Conversation

softr8
Copy link
Contributor

@softr8 softr8 commented Jun 8, 2020

With the implementation of default bill address, we have found that some legacy users are going thought the checkout process without billing address failing at the confirm step, this change makes sure that billing address is present.

I am not totally sure if this should be controlled via config variable but I'm assuming all payment methods require billing address

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change (if needed)

@@ -562,6 +562,13 @@ def ensure_shipping_address
end
end

def ensure_billing_address
return if bill_address && bill_address.valid?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SafeNavigation: Use safe navigation (&.) instead of checking if an object exists before calling the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I fix this? it is barely used in the project

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth fixing as it's the preferred way to do that now that we don't need to support any old Rubies from before that syntax was added.

@jarednorman
Copy link
Member

All payment methods don't necessarily require a billing address. I work with some stores that don't even track billing addresses, so this definitely qualifies as a breaking change that would need to be controlled via configuration.

@softr8
Copy link
Contributor Author

softr8 commented Jun 9, 2020

All payment methods don't necessarily require a billing address. I work with some stores that don't even track billing addresses, so this definitely qualifies as a breaking change that would need to be controlled via configuration.

Right, giving a second thought, it might be worth delegating this decision to the payment methods, if at least one of them require the address, then make it required before payment step

what about something like

def billing_address_required
 payment_methods.detect {|pm|| pm.require_billing_address }
end

def ensure_billing_address
  return unless billing_address_required
  return unless bill_address&.vaild?

  errors.add(:base, I18n.t('spree.bill_address_required'))
  false
end

Then each payment method is responsible for it

@aldesantis
Copy link
Member

@softr8 I like the idea on a theoretical level, but I think that may cause problems with stores doing weird stuff (e.g. offering different payment methods depending on some conditions on the order, which may or may not change after the address step).

I'd feel more comfortable with having an app-level configuration toggle.

If you want to make it super-flexible, you could then also add a method to the order class:

def billing_address_required?
  Spree::Config.billing_address_required
end

That way, it's easy to override the behavior per-order, if needed.

@jarednorman what do you think?

@softr8
Copy link
Contributor Author

softr8 commented Jun 9, 2020

@softr8 I like the idea on a theoretical level, but I think that may cause problems with stores doing weird stuff (e.g. offering different payment methods depending on some conditions on the order, which may or may not change after the address step).

That was exactly my point, get the available payment methods for the order and delegate the validation to them, will try another attempt to see how it looks

@softr8 softr8 force-pushed the force-bill-address branch from 7f8ab99 to 60524b6 Compare June 9, 2020 18:01
@softr8 softr8 force-pushed the force-bill-address branch from 60524b6 to eb5e93c Compare June 9, 2020 18:02
@jarednorman
Copy link
Member

I think @aldesantis's suggestion was to avoid adding the payment method check to core here, but instead have that method delegate to the configuration option so that if you want finer grained control (like if you want to delegate to the payment methods like you have) you can override Spree::Order#billing_address_required?.

I like that solution.

@aldesantis
Copy link
Member

Correct: I think we should avoid doing anything with the payment methods on the order, because it seems too brittle and prone to breaking as a new behavior to introduce in existing stores. Instead, we can use global flag and allow people to override it on Spree::Order if they want to implement any custom logic, such as the one originally implemented by @softr8.

@softr8
Copy link
Contributor Author

softr8 commented Jun 11, 2020

Alright! I guess I tried to port our implementation for everyone but it is not that common, we have payment methods per zone (pay on delivery for example) and it does not require billing address.

@softr8 softr8 force-pushed the force-bill-address branch from eb5e93c to e43f1b1 Compare June 11, 2020 16:21
Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm happy with this.

@softr8 softr8 force-pushed the force-bill-address branch from e43f1b1 to e115647 Compare June 12, 2020 15:06
softr8 added 3 commits June 12, 2020 15:54
It fills required information using defined relationships
and it makes it easier to read when data is removed for scenarios
Spree::Config.billing_address_required now can be used to force billing address
to be required and also gives the ability to stores to add custom logic
Can be controlled via Spree::Config.billing_address_required config flag
globally or can be overriden at the order model e.g. it can
be disabled by default but required if at least one payment method
requires billing address
@softr8 softr8 force-pushed the force-bill-address branch from e115647 to 3cab199 Compare June 12, 2020 20:54
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kennyadsl kennyadsl changed the title Force bill address Allow to specify if billing address is required for orders Jun 22, 2020
@kennyadsl kennyadsl added the changelog:solidus_core Changes to the solidus_core gem label Jun 22, 2020
@kennyadsl kennyadsl merged commit 6645dde into solidusio:master Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants