craftcms / commerce

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

[4.x]: cart/update-cart controller doesn't return succes, errors or error keys when returning as JSON #2933

Closed Antimated closed 2 years ago

Antimated commented 2 years ago

What happened?

Description

Doing a post request to the cart/update-cart controller with the headers Accept: application/json doesn't return the succes/error (and probably more) keys as the docs describe.

In commerce 3 the following data was returned for a successful update-cart response (notice the success key being present):

return $this->asJson([
      'success' => !$this->_cart->hasErrors(),
       $this->_cartVariable => $this->cartArray($this->_cart),
       'message' => $message,
]);

This is how it looks in commerce 4: Success (no success key):

return $this->asModelSuccess(
     $this->_cart,
     $message,
     'cart',
    [
        $this->_cartVariable => $this->cartArray($this->_cart),
    ]
);

Error (no error key):

return $this->asModelFailure(
    $this->_cart,
    $message,
    'cart',
    [
        $this->_cartVariable => $this->cartArray($this->_cart),
    ],
    [
        $this->_cartVariable => $this->_cart,
    ]
);

Not sure if this a bug or if the docs are inaccurate (or perhaps an mistake on my part)

Steps to reproduce

  1. Do a post request to cart/update-cart with 'Accept: application/json' headers
  2. Inspect te json output to not have any success, errors or error keys.

Expected behavior

Response should include a success, error and/or error key.

Actual behavior

Screenshot of post request done with Axios Screenshot 2022-08-12 at 10 37 25

Craft CMS version

Craft Pro 4.2.0.2

Craft Commerce version

4.1.0

PHP version

8.1.5

Operating system and version

Darwin 20.5.0

Database type and version

MySQL 5.7.34

Image driver and version

GD 8.1.5

Installed plugins and versions

martyspain commented 2 years ago

This is expected behaviour. Craft and Commerce 4 have changed how JSON responses are handled, so rather than returning a success key you need to check the status code of the response to determine whether the request was a success (status is 200) or not (status is 400). There's a discussion on the change here and I believe it was mentioned in the release notes, though I can't find that at the moment. It's covered in the upgrade guide for plugins, which is where I was made aware of the change, but unfortunately it's not mentioned in the front-end upgrade guides for Craft or Commerce, which I feel is a bit of an oversight.

The Commerce 4.x docs for controller JSON responses are out of date and show the old JSON response, but the Craft 4.x docs for controller JSON responses are correct.

nfourtythree commented 2 years ago

Thanks for jumping in here @martyspain !

@Antimated I have made an issue over on the docs repo to make some updates/fixes to the docs so this is more clear to people upgrading from a previous version of Commerce.

Thanks!