commerce icon indicating copy to clipboard operation
commerce copied to clipboard

when updating shipping address by ajax, cart.orderLanguage is reset

Open cruiser12 opened this issue 4 years ago • 2 comments

Description

cart.orderLanguage is actually defined by the current sites language. But whenever we update the cart by AJAX when e.g. updating the shipping address, the cart.orderLanguage will always return to the primary site language.

Additional info

PHP version | 7.4.12 MySQL 5.7.32 Craft Pro 3.7.11 Craft Commerce | 3.4.1

Steps to reproduce

  1. Save the cart shipping address and billing address in advance in primary site.
  2. Switch to another site with another language
  3. Then change some value of the address details update them via an AJAX request.

AJAX post parameters:

action: commerce/cart/update-cart
successMessage: 62bc1d15b0bf3dcbc1f39f1913fd899ca6a00e8100fd445c11c34a5ba0830c6bAddresses selected.
redirect: 5c12ff76b54ae0d639e1ad36f3d267e26ec09eb1dca362316e07a0871f2ac77e/fr/shop/checkout?address-saved
CRAFT_CSRF_TOKEN: 9qOdFkkupiFE1IuENcqJIcKMfSir6DXsLo8q3ee6UrP1Q00kOlcmjrOVy0U7duRtFK3jwViez2OIvlB_wbcAg2bnZJ_X2Dr0gg98EU0Fa-0=
shippingAddress[firstName]: test
shippingAddress[lastName]: test
shippingAddress[businessName]: test
shippingAddress[countryId]: 215
shippingAddress[address1]: test
shippingAddress[city]: test
shippingAddress[zipCode]: 4143
shippingAddress[phone]: 041779858543
email: [email protected]
shippingAddress[notes]: 
orderLanguage: fr-CH
makePrimaryBillingAddress: 1
makePrimaryShippingAddress: 1
shippingAddress[id]: 201
billingAddress[id]: 201
billingAddressSameAsShipping: on
billingAddress[firstName]: test
billingAddress[lastName]: test
billingAddress[businessName]: test
billingAddress[countryId]: 215
billingAddress[address1]: test
billingAddress[city]: test
billingAddress[zipCode]: 4143
billingAddress[phone]: 0041779858543
billingAddress[notes]: 

will result in

{
"success":true,
"cart":{
...
orderLanguage: de-CH
...
}}

even the request was sent from a site that has fr-CH site language and also the cart.orderLanguage parameter was set to fr-CH in request.

Its somehere here in vendor/craftcms/commerce/src/services/Carts.php in Line 140

// Load the current cart if there is a cart number in the session
if ($isNumberCartInSession) {
    $number = $this->getSessionCartNumber();
    // Get the cart based on the number in the session.
    // It might be completed or trashed, but we still want to load it so we can determine this and forget it.
    $cart = Order::find()->number($number)->trashed(null)->anyStatus()->withLineItems()->withAdjustments()->one();
}

Maybe if that cart was prior created in a different language ?? How can i update the carts order language afterwards, e.g. when the user decides to switch the language during checkout?

cruiser12 avatar Sep 06 '21 12:09 cruiser12

@cruiser12 The update cart will always return the primary site. This needs improvement to record the site that is being requested. For now you can create your own action method and set the current site by Craft::$app->getSites()->setCurrentSite($siteId); to return the correct language.

pdaleramirez avatar Sep 30 '21 10:09 pdaleramirez

But in that case its a bug, because customers will receive order confirmation emails in the wrong language.

cruiser12 avatar Sep 30 '21 12:09 cruiser12