THELIA Forum

Welcome to the THELIA support and discusssion forum

Offline


Hello , developers,
I have the next  specific case:
                    If user try to add the same product twice , he should get a message like , ```Product is all ready in your cart ``` and should not increase the quantity for it.
What I try :  I create a listener that is listen CART_ADDITEM

   public static function getSubscribedEvents()
    {
        return array(
         TheliaEvents::CART_ADDITEM => array('existProductInCart', 128)
        );
    }
* i try to change number for priority , but  all the time my code if run before the add Item functionality.

Br., Lucian

Offline


Priority should be > 128 (e.g. 250) if you want your listener be called before the standard Thelia one, and lower (e.g. 10) if you want it called after the standard one.

However, if your code is called before the sandard listener, you can stop the event propagation (and prevent the item to be added to the cart) by calling $evetn->stopPropagation()


OpenStudio Toulouse

Offline


Thanks!Work's the only think i need  to manage now is the popup for user notification!
Br., Lucian

Offline


You can throw an exception that will be eventually catched by the CartController, and displayed to your customer.


OpenStudio Toulouse

Offline


not quite, you can't throw an exception because it is not an error is a specific logic that should return a 200 code. the approach is to manage this before the event dispatch.

Offline


If you throw a FormValidationException, the exception will be catched by the cart controller, and you'll get a 200 return code and the error message in the form's context.
You'll then be able to get it in your template using :

{form name="thelia.cart.add" }
    {if $form_error}<div class="alert alert-error">{$form_error_message}</div>{/if}
{/form}

OpenStudio Toulouse