Jump to content

Edit History

elboletaire

elboletaire


Added missing part where I explain how to tune the front to properly get this working

It's been a year, but this answer may help others because this issue still prevails.

The easiest way to block items from being added to a cart is by killing the php process (aka die/exit the code), rn the current implementation does not allow any other way. It would be awesome if just throwing an exception worked, but exceptions are kinda weird in current prestashop's state (both with and without the debug mode enabled btw; it just crashes and the frontend doesn't care about it).

If you want your frontend to not do something strange, you must return a json with an array containing 'hasError' => true. The problem with this, though, is that the frontend js is as terrible as most of prestashop frontend and it does nor properly handle these errors...

public function hookActionCartUpdateQuantityBefore(array $params)
{
    die(json_encode([
        'errors' => 'whatever.. due to how the frontend js is done, this won\'t be shown anyways',
        'hasError' => true,
    ]));
}

So by adding that "hasError" the only thing you're doing is forbidding prestashop from doing any other requests (like the ps_shoppingcart's modal which opens after adding something to a cart), but, again, it won't show anything on your side.

You'll have to tune the theme js in order to properly show this.

In case you're using the default prestashop "theme-classic" or a variant of it (without any js changed), you have the default modules installed (including ps_shoppingcart), and you have ajax installed, you can do another workaround. Instead of returning that 'hasError' => true, you can inject some js to fix the default behavior and properly show the errors.

So, instead of the previous php code, using something like:

public function hookActionCartUpdateQuantityBefore(array $params)
{
    die(json_encode([
        'errors' => ['ok this time we\'re getting the errors to the front end :)'],
    ]));
}

And then, injecting a custom js file with these contents:

let errors = [];
;jQuery(function ($) {
  // here we locally store the errors into our errors var, for later usage.
  prestashop.on('updateCart', function (event) {
    if (event && event.resp && event.resp.errors.length) {
      errors = event.resp.errors;
      prestashop.emit('showErrorNextToAddtoCartButton', { errorMessage: event.resp.errors.join('<br />')});
      return;
    }
    // remove errors contents for any other update without errors
    errors = [];
  })
});
// ps_shoppingcart modal behavior override (actual fix)
prestashop.blockcart.showModal = function (modal) {
  // if we're getting errors, do not show the modal
  if (errors.length) {
    return;
  }

  var $body = $('body');
  $body.append(modal);
  $body.one('click', '#blockcart-modal', function (event) {
    if (event.target.id === 'blockcart-modal') {
      $(event.target).remove();
    }
  });
}

Will properly show an error in the front-end of your prestashop site:

imatge.png.6513a65caa0865865801f095cc660908.png

Note that this time I defined the return key 'errors' as an array instead of a string, this is because the default behavior of prestashop is to consider that as an array (with no checks on it, so just in case we don't break anything else).

elboletaire

elboletaire

It's been a year, but this answer may help others because this issue still prevails.

The easiest way to block items from being added to a cart is by killing the php process (aka die/exit the code), rn the current implementation does not allow any other way. It would be awesome if just throwing an exception worked, but exceptions are kinda weird in current prestashop's state (both with and without the debug mode enabled btw; it just crashes and the frontend doesn't care about it).

If you want your frontend to not do something strange, you must return a json with an array containing `'hasError' => true,`. The problem with this, though, is that the frontend js is as terrible as most of prestashop frontend and it does nor properly handle these errors...

imatge.thumb.png.51754a31fea3ce6ab3d953998e0129d1.png

So by adding that "hasError" the only thing you're doing is forbidding prestashop from doing any other requests (like the ps_shoppingcart's modal which opens after adding something to a cart), but, again, it won't show anything on your side.

You'll have to tune the theme js in order to properly show this.

×
×
  • Create New...