4

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

However, if I put manually an address, I can surely select/edit it on the cart. enter image description here

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 !

Sylvain

1 Answer 1

2

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]);
            }
        );
4
  • 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. Commented Jun 8, 2022 at 11:43
  • @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.
    – aodihis
    Commented Jun 9, 2022 at 3:05
  • 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 ! Commented Jun 9, 2022 at 11:53
  • 1
    Perhaps you need to set the saveShippingAddressOnOrderComplete (craftcms.com/docs/commerce/4.x/…). This should save the address to the user when the order is completed. Commented Mar 22 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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