-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[4.x]: Can not set a shipping address on the order that is not owned by the order. #3842
Comments
Hi @MoritzLost Thanks for reporting, this sounds like a strange one. The setOwner() method on the With your ability to replicate the issue are you able to check if the cart has an ID at the point the code is called? Thanks! |
@nfourtythree Looks like it doesn't have an ID!
It's possible that the cart is created during that very request. From the user's perspective, they're going to the event registration page and then submitting the registration form. The registration form then updates the cart with the event ID ( We also run |
Hi @MoritzLost Thank you for the updated information. Usually the cart has been saved when it is created so it should have an ID by this point. As a bit of a test, when you send the payload could you also send a key of Thanks! |
@nfourtythree Just tried it, looks like that works! When I set Any ideas why that is necessary after the last update? I checked a bit deeper. When the error occurs, Actually, I would expect that the cart is saved here, since $autoSetAddresses = $this->_cart->autoSetAddresses(); I'm setting a |
Hi @MoritzLost Glad using We are constantly trying to make sure Commerce is as performant as possible. This includes making sure we aren't making unnecessary saves of elements. When retrieving the cart we try not to make a save if it is not required. This is in an attempt to avoid having many completely blank carts cluttering the system. With all that said, your case does seem to be an interesting one. In our testing we haven't seen the issue that you raised, this could be just a case of getting the correct combination to be able to replicate it. In regards You mentioned that you are calling your own controller, which of course is absolutely fine but adds in another layer. I would be interested to know if you took that layer out if you would see any difference in behaviour when it comes to the core functionality and the error appearing. We will keep monitoring for anyone else reporting the issue and then we can take a further look into it. Will close the issue for now but if you do get a reliable way to replicate it with just Commerce core then we can reopen an investigate. Thanks! |
@nfourtythree Thanks for the explanation! I just tried reproducing the error without our controller. For that, I replaced our custom controller action with Here's the entire request payload: Most of those parameters are set as hidden inputs ( <form class="event-registration" method="post" action="{{ registration_url }}" accept-charset="UTF-8">
{{ csrfInput() }}
{{ actionInput('commerce/cart/update-cart') }}
{{ hiddenInput('purchasableId', event.defaultVariant.id) }}
{{ hiddenInput('clearLineItems', 1) }}
{{ hiddenInput('qty', 1) }}
{# {{ hiddenInput('forceSave', 1) }} #}
{{ hiddenInput('shippingAddressSameAsBilling', '1') }}
{{ redirectInput(registration_url) }}
{{ successMessageInput('My custom success message') }} The only other form field is a select field for the The user doesn't have a cart at this point, so this request would need to create a new cart and save it, since we're setting I can fix the issue for now using Thanks! |
Hi @MoritzLost Thank you for taking the time to provide this data and feedback. I will reopen the issue and use this information to track down what is happening. Thankfully you can at least have a working project for the moment and hopefully we can get this sorted so you can remove the forcing of the save in the future. Thanks! |
@nfourtythree Thanks for the support! I've pushed the |
What happened?
Description
We recently updated from Commerce 4.6.2 to 4.7.1. After this update, some visitors started to get internal server errors (not quite sure if it's related to the update, but it's likely):
Stack trace:
We use Commerce for an event registration system. In the registration form, we call a custom action on our
ShopController
that runs some preliminary checks to make sure there are no other line items in the order, the request includes a validpurchasableId
that is an available event, etc. Then we let Commerce handle the request by runningcommerce/cart/update-cart
:This worked perfectly before and has never caused any problems. After the update, some visitors started getting internal server errors with the above stack trace in the logs.
The problem comes from
Order::setShippingAddress
(as seen in the stack trace). This line throws the error:I managed to reproduce the issue, in this case the
$address->ownerId
isnull
, causing this problem. I also found where this happens. The user selects one of their addresses in the form asbillingAddressId
, the shipping address is set automatically through a hidden input withshippingAddressSameAsBilling
. InCartController::_setAddresses()
(which is called inCartController::actionUpdateCart()
), the address with that ID is fetched and then duplicated with the owner set to the cart:I put in
Craft::dd($cartBillingAddress)
to check the duplicated address. The$cartBillingAddress->owner
is correctly set to the current cart ($this->_cart
), but the$cartBillingAddress->ownerId
remains empty! That's what's causing the error down the line, when the shipping address is copied over from the billing address.I don't know what exactly causes this to happen. It only happens for some users.
Steps to reproduce
See above.
Craft CMS version
4.13.5
Craft Commerce version
4.7.1
PHP version
8.3
Operating system and version
No response
Database type and version
No response
Image driver and version
No response
Installed plugins and versions
The text was updated successfully, but these errors were encountered: