craftcms / spoke-and-chain

Craft CMS + Craft Commerce demo site.
https://craftcms.com/demo?kind=spokeandchain
BSD Zero Clause License
54 stars 28 forks source link

Can’t save new address as guest #116

Closed mattstein closed 2 years ago

mattstein commented 2 years ago

Description

On both demo versions (3.x and 4.x), a JSON error interrupts the process of saving a new address at /checkout/address. The submitted address does get saved, but you’ll be stuck with the Ajax waiting animation and need to do a hard refresh to see it:

Screen Shot 2022-04-11 at 09 53 11 PM

Screen Shot 2022-04-11 at 09 55 00 PM@2x

Steps to reproduce

  1. Spin up a new Spoke & Chain demo and choose either version.
  2. Start at the demo’s homepage without logging into the control panel.
  3. Click Bikes in the main nav, click your favorite bike, and press Add to cart on its product page.
  4. Click the cart icon at the top right and choose Check Out.
  5. Under “Guest checkout”, enter an email address and press Continue as Guest.
  6. Enter a fun shipping address, un-check Use address for billing, and click Add address.
  7. In the modal thinger, add a new fake address and press Save.
  8. Observe that you are now stuck forever.

Bonus: refresh and your new address will be there.

Additional info

Bugsnag 4.x: https://app.bugsnag.com/pixel-and-tonic-inc/demos?i=sk&m=oc (not seeing a Bugsnag alert for 3.x despite the same behavior)

nfourtythree commented 2 years ago

Thanks @mattstein !

This was a good little two-part issue.

First, it turns out the logic for selecting an address had been moved out of the currentUser and addresses|length conditional. This means the address selection list was showing when it shouldn't have been for guest users. It does mean that we had to wave goodbye to the shrugging man ( ¯\_(ツ)_/¯ ).

Secondly, after making that change the problem you stated still existed for logged in users. Looking into this I think something has changed when it comes to encoding sprig attributes. There were two s-val we were passing data and type. Passing these individually causes a double encoding bug, primarily on data as this is already a JSON encoded string. Making the following change seems to fix the issue.

// Before
s-val:data='{{ data|json_encode }}'
s-val:type='{{ type }}'

// After
s-vals='{{ {data: data, type: type}|json_encode }}'

Have pushed these changes up to the stable branch. Will now look at merging them into the Craft 4 version.