Php 获取自定义电子邮件内容上的自定义电子邮件占位符值

Php 获取自定义电子邮件内容上的自定义电子邮件占位符值,php,wordpress,woocommerce,placeholder,email-notifications,Php,Wordpress,Woocommerce,Placeholder,Email Notifications,我在WooCommerce中创建了自己的电子邮件类。由于我的电子邮件内容中需要一个自定义参数,我已将带有此自定义参数的占位符添加到wc电子邮件触发器函数中: public function trigger( $order_id, $firstname, $lastname, $order = false ) { $this->setup_locale(); if ( $order_id && ! is_a( $order, 'WC_Orde

我在WooCommerce中创建了自己的电子邮件类。由于我的电子邮件内容中需要一个自定义参数,我已将带有此自定义参数的占位符添加到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}' ); ?>
然后我在email课上做这件事:

//Trigger for this email.
add_action( 'trigger_email', array( $this, 'trigger' ), 10, 10 );

之后,您可以返回顶部触发功能。

已更新

电子邮件中的占位符仅用于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.'''$姓氏在模板(内容)中使用时:


它应该有效。在之前的代码中,
$firstname
$lastname
未定义

谢谢您的回答,但我不想显示客户名称。这是一个用户的名字,该用户通过我页面上的按钮触发电子邮件发送。看看我的问题,我会在那里发布更新。对不起,不完全是这样。我添加了一些代码。$order对象变量在其中,但我需要的名称不是来自此变量。如果可以的话,解决方案会像你在回答中描述的那样简单。因为我在那里添加了我的自定义文本,它定义了内容。要在内容中获得名称,我将以parameter@LoicTheAztecPHP致命错误:未捕获错误:不在object contextLet us中时使用$this。
/**
 * 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 );
}
<?php printf( get_option( 'wc_custom_email_info' ), '{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();
}