Best way to notify user after redirect

I’m adding an item to the Merx plugin cart with the following route:

          'pattern' => 'add',
          'method' => 'post|get',
          'action'  => function () {
            $id = get('id');
            $quantity = get('quantity');
            try {
                'id' => $id,
                'quantity' => $quantity,
            } catch (Exception $ex) {
              return $ex->getMessage();

go($id) refreshes the page. I’d like to show the user that something was added to the cart after this. What would be the best way of doing this? The answer is probably AJAX, but can I do it with my current setup?

I tried using a route:after hook but I can’t figure out how to detect the specific route:

      'hooks' => [
        'route:after' => function ($route, $path, $method, $result) {
            return $result;

This dumps string(4) "(.*)" even when the 'add' route is triggered.

I guess when something is added to the cart, this information is stored in the session, so you can get it from there after redirect. But I don’t know what the plugin does, so just guessing.

I’ve added a URL parameter than I can use after the page redirect.

go($id . '?add=true');

I can then get the last added product using $cart->last() from Merx and display an ‘Added to Cart’ notification with JS. Seems to work as I intended, or is there a problem with using a URL Parameter like this?

No, that’s ok.

1 Like