Answers.Body: <p>Did you already run this command when upgrading to commerce 4?</p>
  <pre><code>php craft commerce/upgrade
  </code></pre>
  <p>If I am not wrong this command will do migrate the address also.</p>
  <p>Yes, the craft address is an element now, so we can have separate address in customer and address.
  In our practice for now, to get the customer address recorded and use it on cart we do like this.</p>
  <p>In checkout part for front end instead update the address directly to <code>update-cart</code> end point we do update the address to customer, here is some info for this customer address endpoint:</p>
  <p>-- action : <code>users/save-address</code>
  Field can be use(by default) :</p>
  <p>firstName, lastName, addressLine1, countryCode, administrativeArea, postalCode etc.</p>
  <p>You can update the field on cms -&gt; user settings -&gt; address field.</p>
  <p>So after the address is saved on user, we only pass the addressId to <code>billingAddressId</code> or <code>shippingAddressId</code> to <code>commerce/update-cart</code></p>
  <p>Another alternative is we can listen on some Order event for example like after complete order, then we duplicate the address to customer, something like this:</p>
  <pre><code>Event::on(
              Order::class,
              Order::EVENT_AFTER_COMPLETE_ORDER,
              static function (Event $event) {
                  /** @var Order $order */
                  $order = $event-&gt;sender;
                  Craft::$app-&gt;getElements()-&gt;duplicateElement($order-&gt;billingAddress, ['ownerId' =&gt; $order-&gt;customer-&gt;id]);
              }
          );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-08T11:43:12.667Z
Answers.Text: Maybe I forgot to mention. Yes I ran this commerce/upgrade command and the address followed just fine and I can edit already existing address. The problem is trying to enter new addresses. They are saved in the Shipping/Billing section of the order, but not in the Customer list of addresses.
Answers.UserDisplayName: Sylvain Tremblay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-09T03:05:59.253Z
Answers.Text: @SylvainTremblay, I see yes, we come to this problem also, so our practice now in front end, rather than add the address directly to update cart we add the address to customer (not documented yet I think, but I will update my answer for this) then we pass the address id to update cart.
Answers.UserDisplayName: aodihis
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-09T11:53:56.420Z
Answers.Text: Oh that's a good idea, very clever ! However we got an answer that fulfill our need. We used the EVENT_AFTER_COMPLETE_ORDER thanks to Luke. 
  I wouldn't have found this by myself as I don't know all the possible HOOK names, or even functions like duplicateElement, that's why I appreciate y'all for your help !
  Thanks !
Answers.UserDisplayName: Sylvain Tremblay
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-03-22T20:26:02.423Z
Answers.Text: Perhaps you need to set the `saveShippingAddressOnOrderComplete` (https://craftcms.com/docs/commerce/4.x/addresses.html#save-addresses-when-completing-an-order). This should save the address to the user when the order is completed.
Answers.UserDisplayName: Gary Reckard
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-08T03:32:00.243Z
Answers.LastActivityDate: 2022-06-09T03:17:41.380Z
Answers.LastEditDate: 2022-06-09T03:17:41.380Z
Answers.UserDisplayName: aodihis
Body: <p>we are trying to implement the brand new Commerce 4. We're about to release our Client's site and decided to Go ahead and migrate to Craft 4 before going live...</p>
  <p>The upgrade went pretty smooth except the fact we realized addresses are handled quite differently !!! (to say the less)...</p>
  <p>We're having problem at checkout. All the rest is fine, we can go up to the payment and everything, but I can not manage to get any address attach to the customer. I can see the shipping address and billing address in the order, but not on the customer page :</p>
  <p><a href="https://i.sstatic.net/VZzF6.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/VZzF6.png" alt="enter image description here" /></a></p>
  <p>However, if I put manually an address, I can surely select/edit it on the cart.
  <a href="https://i.sstatic.net/iqfm4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iqfm4.png" alt="enter image description here" /></a></p>
  <p>Also, I figured changing the OwnerID in the database is what I need to do, because if I change it manually it's all fine.</p>
  <p>I looked and the shop/examples but don't find the <em>magic</em> behind this. Is anyone able to point me out what's missing ? Your help always appreciated !</p>
  <p>Sylvain</p>
  
Tags: <commerce4>
Title: Commerce 4 Address Logic
ThreadText: # Commerce 4 Address Logic
  
  (Asked by: username_0 on 2022-06-02)
  
  we are trying to implement the brand new Commerce 4. We're about to release our Client's site and decided to Go ahead and migrate to Craft 4 before going live...
  
  The upgrade went pretty smooth except the fact we realized addresses are handled quite differently !!! (to say the less)...
  
  We're having problem at checkout. All the rest is fine, we can go up to the payment and everything, but I can not manage to get any address attach to the customer. I can see the shipping address and billing address in the order, but not on the customer page :
  
  [![enter image description here](https://i.sstatic.net/VZzF6.png)](https://i.sstatic.net/VZzF6.png)
  
  However, if I put manually an address, I can surely select/edit it on the cart.
  [![enter image description here](https://i.sstatic.net/iqfm4.png)](https://i.sstatic.net/iqfm4.png)
  
  Also, I figured changing the OwnerID in the database is what I need to do, because if I change it manually it's all fine.
  
  I looked and the shop/examples but don't find the *magic* behind this. Is anyone able to point me out what's missing ? Your help always appreciated !
  
  username_0
  
  **Tags:** commerce4
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-08. Score: 2 | Accepted answer
  
  Did you already run this command when upgrading to commerce 4?
  
  ```
  php craft commerce/upgrade
  
  ```
  
  If I am not wrong this command will do migrate the address also.
  
  Yes, the craft address is an element now, so we can have separate address in customer and address.
  In our practice for now, to get the customer address recorded and use it on cart we do like this.
  
  In checkout part for front end instead update the address directly to `update-cart` end point we do update the address to customer, here is some info for this customer address endpoint:
  
  -- action : `users/save-address`
  Field can be use(by default) :
  
  firstName, lastName, addressLine1, countryCode, administrativeArea, postalCode etc.
  
  You can update the field on cms -> user settings -> address field.
  
  So after the address is saved on user, we only pass the addressId to `billingAddressId` or `shippingAddressId` to `commerce/update-cart`
  
  Another alternative is we can listen on some Order event for example like after complete order, then we duplicate the address to customer, something like this:
  
  ```
  Event::on(
              Order::class,
              Order::EVENT_AFTER_COMPLETE_ORDER,
              static function (Event $event) {
                  /** @var Order $order */
                  $order = $event->sender;
                  Craft::$app->getElements()->duplicateElement($order->billingAddress, ['ownerId' => $order->customer->id]);
              }
          );
  
  ```
  
  > **Comments:**
  > **username_0**: Maybe I forgot to mention. Yes I ran this commerce/upgrade command and the address followed just fine and I can edit already existing address. The problem is trying to enter new addresses. They are saved in the Shipping/Billing section of the order, but not in the Customer list of addresses.  
  > **username_1**: @username_0, I see yes, we come to this problem also, so our practice now in front end, rather than add the address directly to update cart we add the address to customer (not documented yet I think, but I will update my answer for this) then we pass the address id to update cart.  
  > **username_0**: Oh that's a good idea, very clever ! However we got an answer that fulfill our need. We used the EVENT_AFTER_COMPLETE_ORDER thanks to Luke.  I wouldn't have found this by myself as I don't know all the possible HOOK names, or even functions like duplicateElement, that's why I appreciate y'all for your help ! Thanks !  
  > **username_2**: Perhaps you need to set the `saveShippingAddressOnOrderComplete` (https://craftcms.com/docs/commerce/4.x/addresses.html#save-addresses-when-completing-an-order). This should save the address to the user when the order is completed.  
  
  ---
  
  