1

I am building an event booking system for a restaurant using Charge 2.0. Everything is working great, but having problems implementing a feature that shows how many spots are left.

An Event channel entry (created by the restaurant) provides parameters such as price, event name, etc and passes them to a Charge form. The Event entry also includes a field that specifies the maximum number of guests.

The Charge form includes a field for specifying a 'number of guests' for the booking. To indicate how many spots are left, we first need to calculate how many guests have already been booked, which is a simple matter of getting the 'numberOfGuests' field from all of the existing Charge entries and summing them. The problem arises when trying to filter the results using a Search parameter, which limits the entries to a specific event (the Event name is passed to the Charge description field). Here is a bit of the code:

{% set q = 'description:' ~ '"' ~ entry.title ~ '"' %}
{% for entry in craft.charge.charges().search(q) %}
   //get the number of guests

I tried a few different things, including a wildcard search, and none of them return any results. Does anyone know if the Charge model supports a Search parameter?

  • Upon further testing it seems the Search parameter will work with custom fields, but seemingly not with the Charge 'description' field. If I print the Charge description field it returns the expected values. – Roi Agneta Jul 17 '16 at 21:48
3

Developer of Charge here.

Yes - Charge supports all the native Craft criteria search parameters for fields.

Like you mentioned, there are a few special fields that aren't supported currently within that. Those are fields like description, notes etc.. The supported direct attributes on the Charge model that can be searched (in addition to all the basic ElementType attributes) are :

  • userId
  • sourceUrl
  • timestamp
  • hash
  • customerId
  • mode
  • meta

This is in addition to all Craft fields you might have attached to the charges.

For example - if I wanted to find all the charges based on a specific customerId (the stripe identifier), it'd look something like this :

{% set charges = craft.charge.charges({ customerId : 'cus_00000000' }) %}

They can be of course be combined, so all charges for that customer only in live mode :

{% set charges = craft.charge.charges({ mode: 'live', customerId : 'cus_00000000' }) %}

If we had a field on the charge model with a name of ourCustomField, we could search for that like :

{% set charges = craft.charge.charges({ search : { query : 'ourCustomField:' ~ someSearchValue }}) %}

And also, we could combine all those into a single thing too:

{% set charges = craft.charge.charges(
   { mode: 'live', 
     customerId : 'cus_00000000', 
     search : { 
        query : 'ourCustomField:' ~ someSearchValue 
   }}) %}

Or in the alternative syntax, that could be:

{% set q = 'ourCustomField:' ~ someSearchValue %}
{% set charges = craft.charge.charges()
      .customerId('cus_00000000')
      .mode('live')
      .search(q) %}
  • 1
    Thanks, Joel. I ended up adding another field to Charge that stores the event entry ID, and all is good. Loving Charge 2.1 - building a fairly complex restaurant booking system around it that includes a ton a features for the restaurant staff to manage reservations, refund cancellations, and much more! – Roi Agneta Jul 20 '16 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.