Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When executing communityStore.addToCart check for a defined function #905

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JeRoNZ
Copy link
Contributor

@JeRoNZ JeRoNZ commented Dec 19, 2024

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.

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.
@Mesuva
Copy link
Collaborator

Mesuva commented Dec 19, 2024

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

ConcreteEvent.publish('StoreOnBeforeAddToCart', {
    'form': form
});

Which would mean that custom code would do something like this:

ConcreteEvent.subscribe('StoreOnBeforeAddToCart', function (form) {
 // whatever
});

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.

@Mesuva
Copy link
Collaborator

Mesuva commented Dec 19, 2024

(PS - maybe that's what you were referring to by 'couldn't get the additional event listeners to play nicely')

@JeRoNZ
Copy link
Contributor Author

JeRoNZ commented Dec 19, 2024

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:

$this->requireAsset('javascript', 'underscore');
$this->requireAsset('javascript', 'core/events');

but that's only any good for V8. V9 has this in cms.js I think. Perhaps CS needs its own events system?

@Mesuva
Copy link
Collaborator

Mesuva commented Dec 19, 2024

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.
But I've also seen examples of some very lightweight equivalents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants