How we cannot allow adding to basket based of some availability rules?

Hello everybody,

We do not want to allow users to add items to cart if the item is not available based on some custom fields logic and not from twig templates.

We started from the controller function ‘addLineItems’ (Shopware\Storefront\Controller\CartLineItemController) then we go further to cart service:

 $cart = $this-\>cartService-\>add($cart, $lineItem, $salesChannelContext);

Is it possible to decorate Shopware\Core\Checkout\Cart\SalesChannel\CartService in order to change the ‘add’ function by adding our logic?

Does anyone have an ideea how we can achive this?

Best regards,

Sorin

 

Hey Sorin,

in the \Shopware\Core\Checkout\Cart\SalesChannel\CartService::add method, an event is dispatched: LineItemAddedEvent
You could subscribe to this event and check the just added lineItem if it fits your custom logic. If not you could remove it again.

Maybe we should add an additional event right at the beginning of the method, so that the item could completely prevented to be added to the cart. 
If you like, we would appreciate a PR from you on GitHub Pull requests · shopware/platform · GitHub
Otherwise you could also create a ticket for that here: https://issues.shopware.com/

Best regards from Schöppingen

cool Michael Telgmann

Hello Michael,

Thank you for your answer. I’m pretty sure that your solution will work on current implementation, but will be great if an event

will be added before adding an item to cart. I’ll place a ticket on https://issues.shopware.com/

 

Best regards,

Sorin

Hello Sorin,

I faced a similar problem, please tell me if you managed to solve it? And if you did, how?

Best regards,

Anastasia

Hi,

that would be very helpfull for us, if an event will be dispatched before adding an item to the cart.

But i cannot find the ticket?

Thanks and best regards,

Werner.