0

Good morning,

I’m unfamiliar with debugging and diagnosing PHP issues (surface-level knowledge at best). I have a client who, from a previous developer, has some custom functions setup in their theme’s functions.php file. One of these functions send an event certificate once a certain product has been purchased to the attendees of said event.

Recently, after upgrading our child theme and running through a few other updates, we’re getting a PHP warning displayed on this page. The emails still send out as expected, however I wanted to check in on this warning before it potentially became a bigger issue.

The warnings are as follows:

Warning: Use of undefined constant purchaser_name - assumed 'purchaser_name' (this will throw an Error in a future version of PHP) in /public_html/wp-content/themes/hr-advisor-child/functions.php on line 293

Warning: Use of undefined constant purchaser_email - assumed 'purchaser_email' (this will throw an Error in a future version of PHP) in /public_html/wp-content/themes/hr-advisor-child/functions.php on line 294

Warning: Use of undefined constant ticket_name - assumed 'ticket_name' (this will throw an Error in a future version of PHP) in /public_html/wp-content/themes/hr-advisor-child/functions.php on line 295

Warning: count(): Parameter must be an array or an object that implements Countable in /public_html/wp-content/plugins/wp-mail-log/classes/capture-mail.php on line 23
-> Sent to Jordan Lovelle<redacted]>.

The code for this function is as follows:

function send_certificate($request)
{
    $event_id = $request['event_id'];
    $event_name = urlencode(get_the_title($event_id));
    $event_date = tribe_get_start_date($event_id, false, 'jS F Y');
    
    $wootickets = Tribe__Tickets_Plus__Commerce__WooCommerce__Main::get_instance();
    $attendees = $wootickets->get_attendees_by_id( $event_id );
    
    echo "Sending certificate. \r\n";
    echo "============================\r\n\r\n";
    
    foreach ( (array) $attendees as $attendee ) {
        if ( $attendee['check_in'] == 1 ) {

            $attendee_name = $attendee[purchaser_name];
            $attendee_email = $attendee[purchaser_email];
            $event_subtitle = $attendee[ticket_name];
            
            $dataArray = array(
                "name"=>$attendee_name, 
                "event_name"=>$event_name, 
            );
           
            $url = "https://<redacted>/wp-content/themes/hr-advisor-child/certificate/getPDF.php?" . http_build_query($dataArray, '', '&', PHP_QUERY_RFC3986);
            error_log($url);
            
            $certificate = url_get_contents($url);
            file_put_contents(get_stylesheet_directory() . "/certificate/certificate.pdf", $certificate);
            
            $to = $attendee_email;
            $subject = "Here's your certificate from the event";
            $headers[] = 'From: <redacted> <no-reply@<redacted>>';
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
            $message = file_get_contents(get_stylesheet_directory() . "/certificate/email.php");
            wp_mail( $to, $subject, $message, $headers, get_stylesheet_directory() . "/certificate/certificate.pdf" );
            
            
            echo "    -> Sent to $attendee_name<$attendee_email>.\r\n";
        }
    }
    
    echo "\r\n\r\n============================\r\n";
    echo "All done. \r\n";
    exit();
}

Any help to understand why these warnings occur and how to fix it would be very much appreciated.

Thanks, Jordan.

Jordan Lovelle
  • 53
  • 1
  • 3
  • 8
  • Does this answer your question? [What does the PHP error message "Notice: Use of undefined constant" mean?](https://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean) – Tangentially Perpendicular Jun 18 '21 at 01:30
  • *clue:* `Warning: Use of undefined constant purchaser_name` - it thinks there is a constant because constants don't have `$` - and lookee here: `$attendee[purchaser_name]`. Change that (and the others) to be like `$attendee[$purchaser_name]` and you're back in business. – Kinglish Jun 18 '21 at 02:31

2 Answers2

1

As the warning says, you probably want to do what it assumed for you. Change

$attendee_name = $attendee[purchaser_name];

to

$attendee_name = $attendee['purchaser_name'];
instance
  • 72
  • 1
  • 6
0

The problem here is, the data you are getting is blank or not defined. Please put below condition to resolve the warning:

$attendee_name = isset($attendee[purchaser_name]) ? $attendee[purchaser_name] : '';
$attendee_email = isset($attendee[purchaser_email]) ? $attendee[purchaser_email] : '';
$event_subtitle = isset($attendee[ticket_name]) ? $attendee[ticket_name]: '';

For the count warning, make sure to use data as an array.

ZealousWeb
  • 1,647
  • 1
  • 10
  • 11
  • While this does make the error go away, the real issue is they didn't prepend their variables with `$`. In general constants look like THIS. – Kinglish Jun 18 '21 at 02:32