Skip to content
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]: Update cart action with clear address params will break next address update with logged in user #3641

Closed
Braedan opened this issue Aug 21, 2024 · 4 comments
Assignees
Labels
bug commerce4 Issues related to Commerce v4 Craft Commerce

Comments

@Braedan
Copy link

Braedan commented Aug 21, 2024

What happened?

Description

When the config autoSetNewCartAddresses is set to true (default) Commerce seems to aggressively attempt to apply an existing user address onto orders if an address is missing, regardless if the cart is new. This can cause issues when trying to clear cart addresses before applying new ones.

Causes error:
Calling unknown method: craft\\commerce\\elements\\Order::getPrimaryBillingAddressId()

I've managed to work around this issue by ensuring that I provide the params CustomerAddressBehavior::rules() is expecting with my request:

isPrimaryShipping: false,
isPrimaryBilling: false,

Cause

CartController::actionUpdateCart() calls getCart() from the Carts service. This causes autoSetAddresses() to run, which will set the _shippingAddress back to the user’s primary address if the order address is empty. The address now has the CustomerAddressBehavior. Later in actionUpdateCart() setShippingAddress() correctly uses the provided address params, but due to this conditional will incorrectly use the Address with CustomerAddressBehavior attached. Validating this address will fail.

Steps to reproduce

Front end example (address in this context can be either shipping or billing):

  1. User has an order with address auto applied
  2. User clears address from order (provide param(s) clearShippingAddress, clearBillingAddress, clearAdresses to commerce/cart/update-cart)
  3. User attempts to apply new address to order (this will error)

Expected behavior

When logged in, providing an order that has no billing / shipping address data with address params, Commerce should either not apply the customer's primary address back onto the order, or ensure that the behavior isn't put onto the new address.

Craft CMS version

4.9.5

Craft Commerce version

4.6.2 (problem also exists on 5.x)

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@Braedan Braedan added bug commerce4 Issues related to Commerce v4 Craft Commerce labels Aug 21, 2024
@lukeholder
Copy link
Member

lukeholder commented Aug 27, 2024

I believe this is due to us setting the ownerId on the duplicated element, and not the whole owner object (which will also set the ID).

I have pushed a quick fix to the 4.x branch: c02ad47 which would be in our next release.

To get the fix early, change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "4.x-dev#c02ad47ed3a6f14378282c96c9f71e8d9896e3d4 as 4.6.9",
  "...": "..."
}

Then run composer update.

@Braedan Could you let me know how you go with this fix?

We will update this ticket once the release is out.

@lukeholder lukeholder self-assigned this Aug 28, 2024
@nfourtythree
Copy link
Contributor

Commerce versions 4.6.10 and 5.0.18 have now been released with this fix included.

Please run composer update in your project to get the latest changes.

Thanks!

@Braedan
Copy link
Author

Braedan commented Aug 29, 2024

Thanks for the fast fix, this works perfectly 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4 Craft Commerce
Projects
None yet
Development

No branches or pull requests

3 participants