Updated
电子邮件中的占位符仅适用于 Woocommerce 中的电子邮件主题
所以你想要做的事情不能以这种方式进行。
相反,您需要对您的trigger()
函数并在类中添加另一个方法:
/**
* Trigger the sending of this email.
*/
public function trigger( $order_id, $firstname, $lastname, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->formatted_name = $firstname . ' ' . $lastname;
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
/**
* Get email content.
*
* @return string
*/
public function get_content() {
$this->sending = true;
if ( 'plain' === $this->get_email_type() ) {
$email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
} else {
$email_content = str_replace( '{full_name}', $this->formatted_name, $this->get_content_html() );
}
return wordwrap( $email_content, 70 );
}
这次你的占位符应该替换为你的动态$firstname . ' ' . $lastname;
在模板(内容)中使用时:
<?php printf( get_option( 'wc_custom_email_info' ), '{full_name}' ); ?>
原答案
尝试以下方法而不是使用WC_Order
method get_formatted_billing_full_name() https://docs.woocommerce.com/wc-apidocs/source-class-WC_Order.html#802-810:
public function trigger( $order_id, $firstname, $lastname, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
$this->placeholders['{full_name}'] = $this->object->get_formatted_billing_full_name();
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
它应该有效。在你的代码之前$firstname
and $lastname
没有定义;