craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
219 stars 170 forks source link

[4.x]: Address validation php error on update-cart method #3579

Closed trevor-cuffe closed 1 month ago

trevor-cuffe commented 3 months ago

What happened?

Description

I'm getting the following error from the server when sending a POST request to the update-cart api route: "message": "Calling unknown method: craft\commerce\elements\Order::getPrimaryBillingAddressId()",

I'm sending the following data: clearNotices: "" fields: {orderNotes: null} orderNotes: null shippingAddress: {...} shippingAddressSameAsBilling: true shippingMethodHandle: "freight"

I've traced the issue, and what appears to be happening is when validating the "isPrimaryBilling" attribute on the address, the address owner is not a User, but an Order element. So the getIsPrimaryBilling() method in CustomerAddressBehavior gets the owner and incorrectly typecasts as a User, tries to run getPrimaryBillingAddressId(), which does not exist on an Order element, hence the server error.

Not sure why but this started happening when I added my first shipping zone, and set up a rule on the shipping method which checks against the shipping zone.

Craft CMS version

4.10.4

Craft Commerce version

4.6.3.1

PHP version

8.0.30

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

-

linear[bot] commented 3 months ago

PT-1942 [4.x]:

nfourtythree commented 2 months ago

Hi @trevor-cuffe

Unfortunately, we are unable to replicate this issue.

Could you detail or screenshot the setup of your shipping rules and zones so we can do a like-for-like comparison?

Thanks

trevor-cuffe commented 2 months ago

Thanks @nfourtythree

I'm doing a bit more testing and I actually think it has something to do with a customer having an address that is both the primary shipping AND primary billing address.

This appears to happen when I am passing the "shippingAddressSameAsBilling = true" flag, as well as a shippingAddress object property. For my purposes, I solved the issue by removing the shippingAddress details when sending the shippingAddressSameAsBilling property. But the php error still occurs with the combination of all of these conditions.

So to clarify, on further testing, I think the shipping rules and zones was a coincidence and does not seem to impact the occurrence of this bug.

Let me know if you're able to replicate the issue with all of these details! If not I can try to provide some further documentation.

trevor-cuffe commented 2 months ago

Actually one more followup - Leaving out the shippingAddress property did not always solve the issue, but sending "clearShippingAddress: true" with the request data along with "shippingAddressSameAsBilling: true" did seem to work around the issue.

nfourtythree commented 2 months ago

Hi @trevor-cuffe

Could you please send the stack trace and any relevant logs to support@craftcms.com and reference this issue?

So that we can look into it further.

Thanks

nfourtythree commented 1 month ago

Closing this issue for now as it has become stale.

If there is any more information, stack traces or ways to replicate this issue please let us know and we can reopen the issue and continue to look into it.

Thanks!