Answers.Body: <p>We achieve this by simply checking the ids of the addresses set on the cart.  Commerce saves the last used shipping and billing address for customers, so you can just use some simple template logic to achieve what you want I think:</p>
  
  <pre><code>        {% set sameAddress = true %}
          {% if cart.shippingAddress and cart.billingAddress %}
              {% if cart.shippingAddress.id != cart.billingAddress.id %}
                  {% set sameAddress = false %}
              {% endif %}
          {% endif %}
  
          &lt;div class="same-address-checkbox"&gt;
              {{ forms.inputCheckbox({
                  type: 'checkbox',
                  id: 'sameAddress',
                  name: 'sameAddress',
                  checked: sameAddress,
                  value: '1',
                  label: 'Use same address for billing address?',
              }) }}
          &lt;/div&gt;
  </code></pre>
  
  <p>One caveat here, if you offer a checkout during registration, you'llw ant to use my plugin for this as otherwise these last used addresses are not copied to the newly created Craft user - <a href="https://github.com/bossanova808/CommerceRegisterOnCheckout" rel="nofollow noreferrer">https://github.com/bossanova808/CommerceRegisterOnCheckout</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-12T07:20:01.513Z
Answers.Text: Thanks Jeremy. Question: So in the case where a user clicks 'use shipping' I assume you're submitting and saving ONLY a shipping address and no billingAddress, correct? And in the future, if a user added a billing address in their account, that would get saved as such. And again, in the future, if they go back to 'use shipping', you'd have to delete the billingAddress, yeah?
Answers.UserDisplayName: Mike
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-12T07:21:02.837Z
Answers.Text: One more question: given the save last address behaviour, if you allow a past-purchaser to edit address details during a future checkout then can you pass in an ID to make certain the edited address details overwrite the old ones so you don't get an ever-expanding list of addresses in their profile?
Answers.UserDisplayName: Mike
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-12T08:03:48.927Z
Answers.Text: When the user clicks 'use shipping' we submit the form to commerce with the `sameAddress` set to 1 - this causes Commerce to save the address and set that Id as the billing ans shipping address on the cart.  So one address is saved, but the id is saved to both the last used shipping and last used billing address.  
  
  Yes, if you let folks edit a past address, you pass the same id back in so that the address is updated rather than a new one created.
Answers.UserDisplayName: Jeremy Daalder
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-12T08:43:05.727Z
Answers.Text: Wow... ok, thanks Jeremy. I totally missed that 'sameAddress' param in the Commerce docs. All makes sense now. =)
Answers.UserDisplayName: Mike
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-12T06:32:16.203Z
Answers.LastActivityDate: 2017-05-12T06:32:16.203Z
Answers.UserDisplayName: Jeremy Daalder
Body: <p>There is a 'Use shipping for billing' field in the Craft Commerce sample templates but it is unclear to me how to use that in the CP or for an account user in their account or for a future checkout.</p>
  
  <p>I cannot find any documentation regarding this field in the Commerce documents. Is this only being used on the sample templates to handle front end display logic?</p>
  
  <p>We want to be able to save this field so that when an account holder logs into their account, we can show their Shipping and Billing address in the correct state (ie. Billing address fields hidden and 'use shipping' checked). This would also apply to future checkouts too.</p>
  
  <p>Is there a way to do this with the Commerce address fields? Or would we have to add a non-Commerce custom field to Users to save this and then add logic accordingly based on that field? And if yes, would that mean a separate ajax post to save that non-Commerce field during a checkout - or would it get saved to the User profile along with everything else?</p>
  
Tags: <plugin-craftcommerce><user-profile>
Title: Commerce: How to implement/save 'Use shipping for billing' field for future orders
ThreadText: # Commerce: How to implement/save 'Use shipping for billing' field for future orders
  
  (Asked by: username_0 on 2017-05-11)
  
  There is a 'Use shipping for billing' field in the Craft Commerce sample templates but it is unclear to me how to use that in the CP or for an account user in their account or for a future checkout.
  
  I cannot find any documentation regarding this field in the Commerce documents. Is this only being used on the sample templates to handle front end display logic?
  
  We want to be able to save this field so that when an account holder logs into their account, we can show their Shipping and Billing address in the correct state (ie. Billing address fields hidden and 'use shipping' checked). This would also apply to future checkouts too.
  
  Is there a way to do this with the Commerce address fields? Or would we have to add a non-Commerce custom field to Users to save this and then add logic accordingly based on that field? And if yes, would that mean a separate ajax post to save that non-Commerce field during a checkout - or would it get saved to the User profile along with everything else?
  
  **Tags:** plugin-craftcommerce, user-profile
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-05-12. Score: 4 | Accepted answer
  
  We achieve this by simply checking the ids of the addresses set on the cart. Commerce saves the last used shipping and billing address for customers, so you can just use some simple template logic to achieve what you want I think:
  
  ```
          {% set sameAddress = true %}
          {% if cart.shippingAddress and cart.billingAddress %}
              {% if cart.shippingAddress.id != cart.billingAddress.id %}
                  {% set sameAddress = false %}
              {% endif %}
          {% endif %}
  
          <div class="same-address-checkbox">
              {{ forms.inputCheckbox({
                  type: 'checkbox',
                  id: 'sameAddress',
                  name: 'sameAddress',
                  checked: sameAddress,
                  value: '1',
                  label: 'Use same address for billing address?',
              }) }}
          </div>
  
  ```
  
  One caveat here, if you offer a checkout during registration, you'llw ant to use my plugin for this as otherwise these last used addresses are not copied to the newly created Craft user - <https://github.com/bossanova808/CommerceRegisterOnCheckout>
  
  > **Comments:**
  > **username_0**: Thanks username_1. Question: So in the case where a user clicks 'use shipping' I assume you're submitting and saving ONLY a shipping address and no billingAddress, correct? And in the future, if a user added a billing address in their account, that would get saved as such. And again, in the future, if they go back to 'use shipping', you'd have to delete the billingAddress, yeah?  
  > **username_0**: One more question: given the save last address behaviour, if you allow a past-purchaser to edit address details during a future checkout then can you pass in an ID to make certain the edited address details overwrite the old ones so you don't get an ever-expanding list of addresses in their profile?  
  > **username_1**: When the user clicks 'use shipping' we submit the form to commerce with the `sameAddress` set to 1 - this causes Commerce to save the address and set that Id as the billing ans shipping address on the cart.  So one address is saved, but the id is saved to both the last used shipping and last used billing address.    Yes, if you let folks edit a past address, you pass the same id back in so that the address is updated rather than a new one created.  
  > **username_0**: Wow... ok, thanks username_1. I totally missed that 'sameAddress' param in the Commerce docs. All makes sense now. =)  
  
  ---
  
  