Skip to content

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

Open
JeRoNZ wants to merge 2 commits into
concretecms-community-store:masterfrom
JeRoNZ:on_before_add_to_cart
Open

When executing communityStore.addToCart check for a defined function#905
JeRoNZ wants to merge 2 commits into
concretecms-community-store:masterfrom
JeRoNZ:on_before_add_to_cart

Conversation

@JeRoNZ

@JeRoNZ JeRoNZ commented Dec 19, 2024

Copy link
Copy Markdown
Contributor

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

Mesuva commented Dec 19, 2024

Copy link
Copy Markdown
Collaborator

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

Mesuva commented Dec 19, 2024

Copy link
Copy Markdown
Collaborator

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

@JeRoNZ

JeRoNZ commented Dec 19, 2024

Copy link
Copy Markdown
Contributor Author

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

Mesuva commented Dec 19, 2024

Copy link
Copy Markdown
Collaborator

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