在 Woocommerce 自定义电子邮件内容上获取自定义电子邮件占位符值
Get custom email placeholder value on Woocommerce custom email content
我在 WooCommerce 中创建了自己的电子邮件 class。因为我的电子邮件内容中需要一个自定义参数,所以我在 wc 电子邮件触发函数中添加了一个带有此自定义参数的占位符:
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}'] = $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();
}
在他之后我在内容 PHP 文件中做了这个:
<?php printf( __( get_option( 'wc_custom_email_info' ) ), '{full_name}' ); ?>
在选项中我写了 %s
以便我可以将全名添加到内容中。但遗憾的是我得到的是占位符的名称而不是内容:
Blaaaaaaa {full_name} blaaaa
但我需要这个:
Blaaaaaaa Joe Martin blaaaa
更新
此处的名称不是订单中的客户名称。这是我通过 do_action
传递的名称,我从按钮触发该名称。因此,当我页面上的某人单击此按钮时,我将获取他的用户 ID 并从单击该按钮的用户那里获取名称。这是我使用的自定义电子邮件触发器:
$user = get_userdata( get_current_user_id() );
$firstname = $user->user_firstname;
$lastname = $user->last_name;
//Trigger email sending
do_action( 'trigger_email', $order_id, $firstname, $lastname );
然后我在电子邮件中这样做 class:
//Trigger for this email.
add_action( 'trigger_email', array( $this, 'trigger' ), 10, 10 );
之后你可以回到顶部触发功能。
已更新
Placeholders in email are only for the email subject in Woocommerce
所以你尝试做的事情不能以这种方式工作。
相反,您需要对 trigger()
函数进行一些更改,并在 class 中添加另一个方法:
/**
* 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
方法 get_formatted_billing_full_name()
:
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
和 $lastname
未定义;
我在 WooCommerce 中创建了自己的电子邮件 class。因为我的电子邮件内容中需要一个自定义参数,所以我在 wc 电子邮件触发函数中添加了一个带有此自定义参数的占位符:
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}'] = $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();
}
在他之后我在内容 PHP 文件中做了这个:
<?php printf( __( get_option( 'wc_custom_email_info' ) ), '{full_name}' ); ?>
在选项中我写了 %s
以便我可以将全名添加到内容中。但遗憾的是我得到的是占位符的名称而不是内容:
Blaaaaaaa {full_name} blaaaa
但我需要这个:
Blaaaaaaa Joe Martin blaaaa
更新
此处的名称不是订单中的客户名称。这是我通过 do_action
传递的名称,我从按钮触发该名称。因此,当我页面上的某人单击此按钮时,我将获取他的用户 ID 并从单击该按钮的用户那里获取名称。这是我使用的自定义电子邮件触发器:
$user = get_userdata( get_current_user_id() );
$firstname = $user->user_firstname;
$lastname = $user->last_name;
//Trigger email sending
do_action( 'trigger_email', $order_id, $firstname, $lastname );
然后我在电子邮件中这样做 class:
//Trigger for this email.
add_action( 'trigger_email', array( $this, 'trigger' ), 10, 10 );
之后你可以回到顶部触发功能。
已更新
Placeholders in email are only for the email subject in Woocommerce
所以你尝试做的事情不能以这种方式工作。
相反,您需要对 trigger()
函数进行一些更改,并在 class 中添加另一个方法:
/**
* 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
方法 get_formatted_billing_full_name()
:
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
和 $lastname
未定义;