3

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?

4

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

  • 1
    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? – Mike May 12 '17 at 7:20
  • 1
    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? – Mike May 12 '17 at 7:21
  • 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. – Jeremy Daalder May 12 '17 at 8:03
  • Wow... ok, thanks Jeremy. I totally missed that 'sameAddress' param in the Commerce docs. All makes sense now. =) – Mike May 12 '17 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.