Linz Darlington
Linz Darlington

Reputation: 671

Too few arguments to function in WooCommerce Email Hook

I'm creating a function in PHP and I want to pass three arguments into it below:

function lnz_wc_emails_memberreceipients($headers, $object, $order) {

$company = $order->billing_company;

$emailreturn = array (
    'Robin' => "[email protected]",
    'Other' => "[email protected], [email protected]"
);

$headers = array();
$headers[] = 'Bcc: your name '.$emailreturn[$company];
$headers[] = 'Content-Type: text/html';
return $headers;

}
add_filter( 'woocommerce_email_headers', 'lnz_wc_emails_memberreceipients', 10, 2);

However, when I try and do this I get an error which suggests that my function is asking for 3 arguments but is only being passed two.

Fatal error: Uncaught ArgumentCountError: Too few arguments to function lnz_wc_emails_memberreceipients(), 2 passed in /home/benefacto/public_html/dev3/wp-includes/class-wp-hook.php on line 300 and exactly 3 expected in /home/benefacto/public_html/dev3/wp-content/plugins/bnfo-custom-emails/bnfo-custom-emails.php:17 Stack trace: #0 /home/benefacto/public_html/dev3/wp-includes/class-wp-hook.php(300): lnz_wc_emails_memberreceipients('Content-Type: t...', 'customer_comple...') #1 /home/benefacto/public_html/dev3/wp-includes/plugin.php(203): WP_Hook->apply_filters('Content-Type: t...', Array) #2 /home/benefacto/public_html/dev3/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(287): apply_filters('woocommerce_ema...', 'Content-Type: t...', 'customer_comple...', Object(WC_Order)) #3 /home/benefacto/public_html/dev3/wp-content/plugins/woocommerce/includes/emails/class-wc-email-customer-completed-order.php(75): WC_Email->get_headers() #4 /home/benefacto/public_html/dev3/wp-includes/class-wp-hook.php(298): WC_Email in /home/benefacto/public_html/dev3/wp-content/plugins/bnfo-custom-emails/bnfo-custom-emails.php on line 17

If I remove either $object OR $order it works just fine! But if I try and include both it throws the wobbly.

Is there anything I can do to pass it all three? Any thoughts much appreciated.

Upvotes: 2

Views: 5075

Answers (1)

user378704
user378704

Reputation:

Replace

add_filter( 'woocommerce_email_headers', 'lnz_wc_emails_memberreceipients', 10, 2);

With

add_filter( 'woocommerce_email_headers', 'lnz_wc_emails_memberreceipients', 10, 3);

which accepts 3 arguments for the filter woocommerce_email_headers

Upvotes: 10

Related Questions