That's done it - Other than the items you mentioned, the only thing I had to do was to change variationID to ticketsID for ticketsSold as total-tickets-sold did not update on the first try. Final code is as below...
add_action('cred_save_data_248', 'calculate_sold_tickets', 100, 2);
function calculate_sold_tickets($post_id, $form_data) {
$variationID = get_post_meta($post_id, '_wpcf_belongs_ticket-variation_id', true);
$variationsAvailable = get_post_meta($variationID, 'wpcf-total-available-for-variation', true);
$variationsSold = get_post_meta($variationID, 'wpcf-total-sold-for-variation', true);
$ticketID = get_post_meta($variationID, '_wpcf_belongs_ticket_id', true);
$ticketsAvailable = get_post_meta($ticketID, 'wpcf-total-tickets-available', true);
$ticketsSold = get_post_meta($ticketID, 'wpcf-total-tickets-sold', true);
$soldPlaces = $_POST['wpcf-number-of-places-booked'];
$siblingVariations = get_posts(array('post_type' => 'ticket-variation', 'meta_key' => '_wpcf_belongs_ticket_id', 'meta_value' => $ticketID, 'numberposts' => -1));
foreach ($siblingVariations as $sibling) {
update_post_meta($sibling->ID, 'wpcf-total-available-for-variation', $variationsAvailable - $soldPlaces);
}
update_post_meta($variationID, 'wpcf-total-sold-for-variation', $variationsSold + $soldPlaces);
update_post_meta($ticketID, 'wpcf-total-tickets-available', $ticketsAvailable - $soldPlaces);
update_post_meta($ticketID, 'wpcf-total-tickets-sold', $ticketsSold + $soldPlaces);
}
Following on from this, there are two items that I should implement...
1. validation using cred_form_validate, meaning that it's not possible to submit the Booking form for more places than there are available for the parent Variation
Could you advise on how to do this? I've looked into the code but it's a little beyond me. If I see an example for Booking I can then likely implement that to limit Variation stock under a Parent Ticket also.
2. In the instance where, under the same Ticket, there is 4 of Variation A available and 6 of Variation B available, it would be useful if the stock for Variation A was not reduced until the stock for Variation B became less than the stock for Variation A.
If this is not possible, or is too complicated, we will likely set Variation stock to be automatically equal to the Parent Ticket stock on Variation creation and get users to only set stock at the Ticket level. Do please let me know your thoughts on what may be the best approach.