Answers.Body: <p>I have not yet had to do this in Craft 5, but the following is how I did it in Craft 4 - it required some custom module code to hook into the <code>EVENT_AFTER_SAVE_ELEMENT</code> event which triggers when a User is registered. You might also be able to use <code>EVENT_AFTER_ASSIGN_USER_TO_DEFAULT_GROUP</code> instead.</p>
  <pre class="lang-php prettyprint-override"><code>
  use Craft;
  use yii\base\Event;
  use craft\services\Elements;
  use craft\events\ElementEvent;
  use craft\elements\Address;
  
  ...
  
  // --------------------------------------------
  // SAVE ADDRESS AFTER REGISTRATION
  // --------------------------------------------
  
  Event::on(
      Elements::class,
      Elements::EVENT_AFTER_SAVE_ELEMENT,
      function(ElementEvent $e) {
          // bail out on console requests
          if (Craft::$app-&gt;request-&gt;isConsoleRequest) { return; }
  
          // Bail out on CP requests
          if (Craft::$app-&gt;request-&gt;isCpRequest) { return; }
  
          // bail out if it's not a user element being saved, just to triple check ourselves
          if (!$e-&gt;element instanceof \craft\elements\User) { return; }
  
          // else continue...
          $add1 = Craft::$app-&gt;request-&gt;getBodyParam('address');
  
          $userId = $e-&gt;element-&gt;id;
          $address = new Address();
  
          $address-&gt;ownerId = $userId;
          $address-&gt;title = 'Address';
          $address-&gt;fullName = '';
          $address-&gt;addressLine1 = $add1['addressLine1'];
          $address-&gt;addressLine2 = $add1['addressLine2'];
          $address-&gt;locality = $add1['locality'];
          $address-&gt;postalCode = $add1['postalCode'];
          $address-&gt;countryCode = 'GB';
          $address-&gt;administrativeArea = $add1['administrativeArea'];
  
          // Save the address
          Craft::$app-&gt;getElements()-&gt;saveElement($address);
      }
  );
  </code></pre>
  <p>The registration form is as normal, with <code>{{ actionInput('users/save-user') }}</code> and then you can name the address inputs however you like e.g. <code>name=&quot;address[addressLine1]&quot;</code> and then wire them up in the function above. This code actually originally came from a Craft 3 site where it was even more difficult if I recall (as Addresses were part of Commerce but not really part of core Craft), so it's possible there might even be a more native way now - I just went for the simplest possible upgrade route from 3 to 4. Hopefully it can at least help you get it working in 5.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-28T09:38:06.917Z
Answers.LastActivityDate: 2024-07-28T09:38:06.917Z
Answers.UserDisplayName: James Smith
Body: <p>I am working on a craftcms + commerce project and I need to add a shipping address to new user during its registration process. The idea is to have a shipping address added to any newly registered user (no billing address needed).</p>
  <p>Is it possible to add shipping address fields in the same registration process or do i need to create 2 step form or something?</p>
  
Tags: <plugin-craftcommerce><craft5>
Title: Add shipping address to user on registration
ThreadText: # Add shipping address to user on registration
  
  (Asked by: username_0 on 2024-07-28)
  
  I am working on a craftcms + commerce project and I need to add a shipping address to new user during its registration process. The idea is to have a shipping address added to any newly registered user (no billing address needed).
  
  Is it possible to add shipping address fields in the same registration process or do i need to create 2 step form or something?
  
  **Tags:** plugin-craftcommerce, craft5
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-07-28. Score: 1
  
  I have not yet had to do this in Craft 5, but the following is how I did it in Craft 4 - it required some custom module code to hook into the `EVENT_AFTER_SAVE_ELEMENT` event which triggers when a User is registered. You might also be able to use `EVENT_AFTER_ASSIGN_USER_TO_DEFAULT_GROUP` instead.
  
  ```php
  
  use Craft;
  use yii\base\Event;
  use craft\services\Elements;
  use craft\events\ElementEvent;
  use craft\elements\Address;
  
  ...
  
  // --------------------------------------------
  // SAVE ADDRESS AFTER REGISTRATION
  // --------------------------------------------
  
  Event::on(
      Elements::class,
      Elements::EVENT_AFTER_SAVE_ELEMENT,
      function(ElementEvent $e) {
          // bail out on console requests
          if (Craft::$app->request->isConsoleRequest) { return; }
  
          // Bail out on CP requests
          if (Craft::$app->request->isCpRequest) { return; }
  
          // bail out if it's not a user element being saved, just to triple check ourselves
          if (!$e->element instanceof \craft\elements\User) { return; }
  
          // else continue...
          $add1 = Craft::$app->request->getBodyParam('address');
  
          $userId = $e->element->id;
          $address = new Address();
  
          $address->ownerId = $userId;
          $address->title = 'Address';
          $address->fullName = '';
          $address->addressLine1 = $add1['addressLine1'];
          $address->addressLine2 = $add1['addressLine2'];
          $address->locality = $add1['locality'];
          $address->postalCode = $add1['postalCode'];
          $address->countryCode = 'GB';
          $address->administrativeArea = $add1['administrativeArea'];
  
          // Save the address
          Craft::$app->getElements()->saveElement($address);
      }
  );
  
  ```
  
  The registration form is as normal, with `{{ actionInput('users/save-user') }}` and then you can name the address inputs however you like e.g. `name="address[addressLine1]"` and then wire them up in the function above. This code actually originally came from a Craft 3 site where it was even more difficult if I recall (as Addresses were part of Commerce but not really part of core Craft), so it's possible there might even be a more native way now - I just went for the simplest possible upgrade route from 3 to 4. Hopefully it can at least help you get it working in 5.
  
  
  ---
  
  