I have inherited a PDF voucher plugin and would like to fetch and display custom order data in the woocommerce email template. This is the original email code. I would like to display the customer billing first name. <?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?> <p><?php printf( _n( "Hi there. You've been sent a voucher!", "Hi there. You've been sent %d vouchers!", $voucher_count, 'woocommerce-pdf-product-vouchers' ), $voucher_count ); ?></p> <?php do_action( 'woocommerce_email_footer', $email ); ?> Having a look through the developer documentation, we should be able to query the order object using $voucher->get_order(). I've tried with that and the following, but it's returning nothing. $order = wc_get_order( $order_id ); $first_name = $order->get_billing_first_name(); echo $first_name; and I tried: <?php do_action( 'woocommerce_email_header', $email_heading, $email ); $order = $email->object; $first_name = $order->get_billing_first_name; echo $first_name; ?> What am I missing?
Jese Leos
August 19, 2024
Verified user
On WooCommerce PDF Product Vouchers plugin voucher-recipient.php template file, you can see: /** * Voucher recipient html email * * @type \WC_Order $order the order object associated with this email * @type string $email_heading the configurable email heading * @type int $voucher_count the number of vouchers being attached * @type string $message optional customer-supplied message to display * @type string $recipient_name optional customer-supplied recipient name * @type \WC_PDF_Product_Vouchers_Email_Voucher_Recipient $email the email being sent * * @version 3.5.3 * @since 1.2 */ Which means that the WC_Order object $order is already available in this template. So first, copy this template file in your child theme, inside a "woocommerce" folder > "emails" subfolder, to be able to override it. Once done, open edit the copied file, and replace the line: <p><?php printf( _n( "Hi there. You've been sent a voucher!", "Hi there. You've been sent %d vouchers!", $voucher_count, 'woocommerce-pdf-product-vouchers' ), $voucher_count ); ?></p> with: <p><?php printf( __("Hi %s. ", 'woocommerce-pdf-product-vouchers'), $order->get_billing_first_name() ) . printf( _n( "You've been sent a voucher!", "You've been sent %d vouchers!", $voucher_count, 'woocommerce-pdf-product-vouchers' ), $voucher_count ); ?></p> Save. It should now display the customer first name.