PHP WordPress Filter Function Not Modifying Email Contents for The Events Calendar email tickets

67 views Asked by At

The Events Calendar (Event Tickets Plus Plugin) sends tickets to your customers once a WooCommerce order has been completed. I want to conditionally exclude certain tickets from the email being sent (I want some tickets to be collected at the venue entrance).

To do so, I have implemented a custom function, hooked to tribe_tickets_ticket_email_recipient, that modifies the $tickets array to exclude specific ticket data. However, even though my function correctly modifies the $tickets array (confirmed via var_dump), the email still includes all tickets, as if my changes are not applied.

Code:

Here is my custom function:

add_filter( 'tribe_tickets_ticket_email_recipient', 'disable_specific_ticket_email', 1, 6 );

function disable_specific_ticket_email( $to, $post_id, $order_id, &$tickets, $provider, $args ) {
    
    // Dump initial state of $tickets
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/initial_var_dump.txt', var_export($tickets, true));
    
    $excluded_tickets = [];

    foreach ( $tickets as $key => $ticket ) {
        if (isset($ticket['event_id']) && isset($ticket['product_id'])) {
            $event_id = $ticket['event_id'];
            $product_id = $ticket['product_id'];

            // Check if this ticket should be excluded from the email
            if (
                ($event_id == 69629 && $product_id == 69632) ||
                ($event_id == 69641 && $product_id == 69644) ||
                ($event_id == 69651 && $product_id == 69660) ||
                ($event_id == 69833 && $product_id == 69842) ||
                ($event_id == 69801 && $product_id == 69816) ||
                ($event_id == 69994 && $product_id == 69997) ||
                ($event_id == 70006 && $product_id == 70008) ||
                ($event_id == 69744 && $product_id == 69753) ||
                ($event_id == 69755 && $product_id == 69758) ||
                ($event_id == 69765 && $product_id == 69770) ||
                
                ($event_id == 69675 && ($product_id == 69686 || $product_id == 69687))
            ) {
                $excluded_tickets[] = $key;
            }
        }
    }

    // Remove excluded tickets from the original $tickets array
    foreach ($excluded_tickets as $key) {
        unset($tickets[$key]);
    }
    

    // Dump final state of $tickets
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/final_var_dump.txt', var_export($tickets, true));
    
    // If there are still tickets left, send the email
    if (count($tickets) > 0) {
        return $to;
    }

    // If no tickets are left, disable the email
    return '';
}


What I've Tried:

I've confirmed that the $tickets array is being modified as expected via var_dump. It's removing the tickets I want to exclude. I've tried altering the priorities of add_filter, but no luck. It seems that the email is being sent before I alter the tickets array.

Question(s):

Why is the email is still sent with all the tickets, despite the $tickets array being modified correctly within my function?

Is there another place where I should be applying this logic to ensure the email sent reflects the changes?

Other Thoughts :

I can't find the code in the plugin that sends the email tickets, if I could find that maybe I could check the contents of the $tickets array right before its sent, and then set my function to run in priority before this?

Notes: This is also discussed on The Event Calendar website at the bottom of the page linked below. Note its the legacy emails I'm using, I'm not using the new email system they have. There is an option to enable it, which I did not. Regardless that allows very little customisation in the settings. https://theeventscalendar.com/knowledgebase/disable-the-ticket-email/

Any help is appreciated.

0

There are 0 answers