solidusio-contrib / solidus_zip_zones

Add the ability to extract zones by zip code
BSD 3-Clause "New" or "Revised" License
3 stars 3 forks source link

Zip+4 doesn't match zone #5

Open embold-given opened 2 years ago

embold-given commented 2 years ago

We're building zones based on zip for the purposes of taxing. If a customer checks out using their zipcode+4 (44311-4401 vs 44311) then the zone doesn't match and they don't get charged tax. I'm sure this is a mainly US issue, the +4 on a zip code is generally optional (I don't know many people who know theirs), but when we do address validation through a service like FedEx the zip+4 is what comes back, so it's definitely something that can happen.

embold-given commented 2 years ago

Found a solution: I monkeypatched app/models/solidus_zip_zones/zone_decorator.rb and changed this

      base.scope :with_member_ids, ->(state_ids, country_ids, zipcode) do
        if !state_ids.present? && !country_ids.present? && !zipcode.present?
          none
        else
          spree_zone_members_table = Spree::ZoneMember.arel_table

to this:

      base.scope :with_member_ids, ->(state_ids, country_ids, zipcode) do
        if !state_ids.present? && !country_ids.present? && !zipcode.present?
          none
        else
          if country_ids==233
            zipcode = zipcode.split('-').first
          end
          spree_zone_members_table = ::Spree::ZoneMember.arel_table

Since I'm not currently sure if there are any countries that have a legitimate - in their zip code structure, I limited the changes to addresses in the US.

At some point I think it would be neat to try to add a checkbox to the zone to match the +4 if required instead of just flat-out ignoring it, but for now my use case requires that I strip it from the string.

kennyadsl commented 2 years ago

Thanks for reporting this (and the solution), I wasn't aware of this +4 zip codes thing. Let's stick this issue here for now, but a contribution is definitely welcome if you have time!