|
| 1 | +# Addresses |
| 2 | + |
| 3 | +The `Spree::Address` model is used to track address information for customers. |
| 4 | +Addresses are consumed by `Spree::Order`s, `Spree::Shipment`s, and |
| 5 | +`Spree::Carton`s. |
| 6 | + |
| 7 | +If your store uses the [`solidus_auth_devise`][solidus-auth-devise] gem, |
| 8 | +customers belong to the `Spree::User` model. A customer may have multiple |
| 9 | +addresses. For example, it is typical for customers to have separate billing and |
| 10 | +shipping addresses. |
| 11 | + |
| 12 | +You may want to find all of the addresses associated with a `Spree::User`: |
| 13 | + |
| 14 | +```ruby |
| 15 | +Spree::User.find(1).addresses |
| 16 | +``` |
| 17 | + |
| 18 | +`Spree::Address` objects have the following attributes: |
| 19 | + |
| 20 | +- `firstname`: The first name for the person at this address. |
| 21 | +- `lastname`: The last name for the person at this address. |
| 22 | +- `address1` and `address2`: The street address (with an optional second line). |
| 23 | +- `city`: The city where the address is. |
| 24 | +- `zipcode`: The postal code. |
| 25 | +- `phone` and `alternative_phone`: The customer's phone number(s). |
| 26 | +- `state_name`: If the customer uses a region name that doesn't correspond with |
| 27 | + a country's list of states, the address can store the user-entered |
| 28 | +- `state_name` as a fallback. |
| 29 | +- `alternative_phone`: The alternative phone number. |
| 30 | +- `company`: A company name. |
| 31 | +- `state_id` and `country_id`: IDs for the `Spree::State` and `Spree::Country` |
| 32 | + objects associated with the customer's entered address. These are used to |
| 33 | + determine the customer's [zone][zones], which determines applicable taxation |
| 34 | + and shipping methods. |
| 35 | + |
| 36 | +For more information about how countries, states, and zones work in Solidus, see |
| 37 | +the [Locations][locations] documentation. |
| 38 | + |
| 39 | +[locations]: ../locations/index.md |
| 40 | +[solidus-auth-devise]: https://github.com/solidusio/solidus_auth_devise |
| 41 | +[zones]: ../locations/zones.md |
| 42 | + |
| 43 | +## Countries and states |
| 44 | + |
| 45 | +Countries and states can affect both taxation and shipping on orders. So, an |
| 46 | +address must always link to a `Spree::Country` object. Because some countries do |
| 47 | +not have associated `Spree::State`s, a state object is not required. |
| 48 | + |
| 49 | +If the user-entered state does not correspond with a `Spree::Country`'s |
| 50 | +associated states, then the `state_name` attribute is used to record the state |
| 51 | +name. |
| 52 | + |
| 53 | +If you use the `solidus_frontend` gem to provide your store's frontend, the |
| 54 | +state field is hidden if the customer's country does not have `Spree::State`s |
| 55 | +associated with it. |
| 56 | + |
| 57 | +## Required address values |
| 58 | + |
| 59 | +By default, `Spree::Address` objects require many address values, including a |
| 60 | +phone number and zip code value. |
| 61 | + |
| 62 | +You may want to alter Solidus's address requirements for your store. For |
| 63 | +example, if you do not require customer phone numbers in order for them to check |
| 64 | +out. |
| 65 | + |
| 66 | +Right now, you need to [monkey-patch][monkey-patch] the `Spree::Address` model |
| 67 | +in order to change its requirements. For example, you could prepend your custom |
| 68 | +behavior that redefines `Spree::Address`'s `require_phone?` method: |
| 69 | + |
| 70 | +```ruby |
| 71 | +module PhoneNotRequired |
| 72 | + def require_phone? |
| 73 | + false |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +Spree::Address.prepend(PhoneNotRequired) |
| 78 | +``` |
| 79 | + |
| 80 | +Similarly, if you ship to countries that don't require postal codes, like Hong |
| 81 | +Kong or Macau, you may want to make postal codes optional instead of required. |
| 82 | + |
| 83 | +Right now, you can monkey-patch the `Spree::Address` model in order to remove or |
| 84 | +change the requirements. For example, you could prepend your own custom behavior |
| 85 | +that redefines `Spree::Address`'s `require_zipcode?` method: |
| 86 | + |
| 87 | +```ruby |
| 88 | +module ZipCodeValidation |
| 89 | + def require_zipcode? |
| 90 | + # if a country that you ship to does not require postal codes, add its iso |
| 91 | + # code to the following array so that Spree::Address does not require zip |
| 92 | + # codes for addresses in those countries. |
| 93 | + !['HK','MO'].include?(country.iso) |
| 94 | + end |
| 95 | +end |
| 96 | + |
| 97 | +Spree::Address.prepend(ZipCodeValidation) |
| 98 | +``` |
| 99 | + |
| 100 | +<!-- TODO: |
| 101 | + Ideally, we do not want to recommend monkey-patching the Spree::Address model. |
| 102 | + It would be great make address requirements more configurable in general. |
| 103 | + Then, we can revisit this documentation. |
| 104 | +--> |
| 105 | + |
| 106 | +[monkey-patch]: https://en.wikipedia.org/wiki/Monkey_patch |
0 commit comments