Using Statamic, I have a collection called courses. Within this collection is replicator field which contains two fields in each set. Course Date and Total Delegates.
People can book courses for a specific course date. When this booking is successful, I just need to update the Total Delegates value by however many people they booked for.
The blueprint and content query so far is below. I’m simply trying to query the collection and set the specific course upcoming course total delegates field. I’m doing this within a controller using Statamic’s Entry facade.
Ignore that total delegates isn’t here, it’s not set for all entries yet. I’m testing on an entry that does have it.
upcoming_courses:
-
id: lzr99kyj
course_date: '2024-08-22'
type: course
enabled: true
-
id: lzvpgjuy
course_date: '2024-08-15'
type: course
enabled: true
Content query in controller so far
$delegates_booked = $checkout_session->metadata->delegate_count;
$upcoming_course_date_id = $checkout_session->metadata->upcoming_course_date_id;
$course_id = $checkout_session->metadata->course_booked_id;
$entry = Entry::query()
->where('collection', 'courses')
->where('id', $course_id)
->first();
$upcoming_courses = $entry->get('upcoming_courses');
$values[0]['booked_delegates'] = $values[0]['booked_delegates'] + $delegates_booked;
$entry->set('upcoming_courses', $values);
$entry->save();
It looks like you’re most of the way there, but it looks like you’re setting the wrong variable. Try:
$upcoming_courses = $entry->get('upcoming_courses');
$upcoming_courses[0]['booked_delegates'] = $upcoming_courses[0]['booked_delegates'] + $delegates_booked;
$entry->set('upcoming_courses', $upcoming_courses);
$entry->save();