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

successMessage and failMessage don't work for JSON requests #1871

Closed
OwenMelbz opened this issue Nov 13, 2020 · 1 comment
Closed

successMessage and failMessage don't work for JSON requests #1871

OwenMelbz opened this issue Nov 13, 2020 · 1 comment
Assignees

Comments

@OwenMelbz
Copy link
Contributor

OwenMelbz commented Nov 13, 2020

When you send a hashed successMessage or failMessage to update the cart, the value does not get used for JSON responses.

The code located:

if (($cartUpdatedNotice = $this->request->getParam('cartUpdatedNotice')) !== null) {

if (($cartUpdatedNotice = $this->request->getParam('cartUpdatedNotice')) !== null) {
    $cartUpdatedMessage = Html::encode($cartUpdatedNotice);
    Craft::$app->getDeprecator()->log('cartUpdatedNotice', 'The `cartUpdatedNotice` param has been deprecated for `carts/*` requests. Use a hashed `successMessage` param instead.');
} else {
    $cartUpdatedMessage = Craft::t('app', 'Cart updated.');
}

if ($request->getAcceptsJson()) {
    return $this->asJson([
        'success' => !$this->_cart->hasErrors(),
        $this->_cartVariable => $this->cartArray($this->_cart),
        'message' => $cartUpdatedMessage,
    ]);
}

$this->setSuccessFlash($cartUpdatedMessage);

As the line $cartUpdatedMessage = Craft::t('app', 'Cart updated.'); sets the variable to "Cart updated"

It then directly references the variable 'message' => $cartUpdatedMessage, which then passes "Cart updated" back through the response regardless of the value.

When you look inside $this->setSuccessFlash($cartUpdatedMessage);

You can see it does the $message = $this->request->getValidatedBodyParam('successMessage') ?? $default;

Which would convert the body param into the value you supplied.

But as this code is never executed you get the hardcoded response for all JSON requests.

Thanks :)

UPDATE: I've found the failMessage doesn't get validated or converted by either the JSON or text/html responses e.g. the code does rather than setting the flash message.

$error = Craft::t('commerce', 'Unable to update cart.');

if ($request->getAcceptsJson()) {
    return $this->asJson([
        'error' => $error,
        'errors' => $this->_cart->getErrors(),
        'success' => !$this->_cart->hasErrors(),
        'message' => $error,
        $this->_cartVariable => $this->cartArray($this->_cart)
    ]);
}

Craft::$app->getUrlManager()->setRouteParams([
    $this->_cartVariable => $this->_cart
]);

Craft::$app->getSession()->setError($error);
@OwenMelbz OwenMelbz added the New label Nov 13, 2020
@OwenMelbz OwenMelbz changed the title successMessage doesn't work for Json requests successMessage doesn't work for JSON requests Nov 13, 2020
@OwenMelbz OwenMelbz changed the title successMessage doesn't work for JSON requests successMessage and failMessage don't work for JSON requests Nov 13, 2020
@pdaleramirez pdaleramirez self-assigned this Nov 17, 2020
@pdaleramirez
Copy link
Contributor

Fixed for next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants