When executing communityStore.addToCart check for a defined function - #905
When executing communityStore.addToCart check for a defined function#905JeRoNZ wants to merge 2 commits into
Conversation
onBeforeAddToCart and execute that first, returning without adding if the function returns boolean false. The function may modify the passed form argument, pushing values into a hidden form order attribute etc.
|
I like what you're looking to do here, it makes sense to just have some javascript hooks in place to be able to trigger some extra bits and pieces. My only hesitation is that this just relies on global scope for a function, and Concrete does have a events publish/subscribe system. Could we do something like Which would mean that custom code would do something like this: It would need some work to have some way to stop further processing, like you have now with the boolean response. But I think that could be done by passing in a variable to change. But maybe this is only available in the dashboard or when the UI is loaded in, but I think it's worth checking if this is available, as it then is a bit more flexible. |
|
(PS - maybe that's what you were referring to by 'couldn't get the additional event listeners to play nicely') |
|
I've revised the code, but I just reread your comment about the dashboard, and seems it's not present when not logged in. Rats. Looks like all the blocks and page controllers will need: but that's only any good for V8. V9 has this in cms.js I think. Perhaps CS needs its own events system? |
|
Maybe it does need it's own events system, but I'd be wondering if it's just adding complexity. A library like https://github.com/mroderick/PubSubJS might be the way to go. |
onBeforeAddToCart and execute that first, returning without adding if the function returns boolean false. The function may modify the passed form argument, pushing values into a hidden form order attribute etc.
The reason I did this is that I needed to use the cart for an event booking system, where the user can choose from a list of available dates, or all dates with a correspondingly variable price. I also have to check that there are no more than 100 users registered for a particular day, and that gets tricky when they could be picking one day or all days, so some rather
tortuous back end logic needs to happen first)
I couldn't get the additional event listeners to play nicely, so I figured the tidiest solution here was to make addToCart check for the existence of a custom function (defined in the view) and execute that first, bailing out if it returns false.