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

时间:2023-01-17
本文介绍了在 Woocommerce 自定义电子邮件内容上获取自定义电子邮件占位符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 WooCommerce 中创建了自己的电子邮件类.因为我需要在我的电子邮件内容中使用自定义参数,所以我在 wc 电子邮件触发器函数中添加了一个带有此自定义参数的占位符:

I've created my own email class in WooCommerce. Because I need a custom parameter in my email content, I've added a placeholder with this custom parameter to the wc email trigger function:

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 文件中做了这个:


After his I've did this in the content PHP file:

<?php printf( __( get_option( 'wc_custom_email_info' ) ), '{full_name}' ); ?>

在选项中我写了 %s 以便我可以将全名添加到内容中.但遗憾的是,我得到的是占位符的名称,而不是内容:

In the option I've wrote %s so that I can add the full name into to content. But sadly I'm getting the name of the placeholder and not the content:

Blaaaaaaa {full_name} blaaaa

Blaaaaaaa {full_name} blaaaa

但我需要这个:

Blaaaaaaa 乔·马丁 blaaaa

Blaaaaaaa Joe Martin blaaaa

更新

此处的名称不是订单中的客户名称.这是我通过从按钮触发的 do_action 传递的名称.因此,当我页面上的某人单击此按钮时,我正在获取他的用户 ID 并从单击该按钮的用户那里获取名称.这是我使用的自定义电子邮件触发器:

Update

The name here is not the customer name from the order. This is a name which I pass through a do_action which I trigger from a button. So when someone on my page clicks this button, I'm fetching his user id and get the name from the user who clicked the button. This is the custom email trigger I use:

$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 );

然后我在电子邮件类中这样做:

Then I do this in the email class:

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

此后您可以返回顶部触发功能.

After this you can go back to the top trigger function.

推荐答案

更新

电子邮件中的占位符仅用于 Woocommerce 中的电子邮件主题

Placeholders in email are only for the email subject in Woocommerce

所以你尝试做的事情不能以这种方式工作.

So what you are trying to do can't work this way.

相反,您需要对 trigger() 函数进行一些更改,并在类中添加另一个方法:

Instead you will need to make a little change in your trigger() function and adding another method in your 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; 在您的模板(内容)中使用时:

This time your placeholder should be replaced with your dynamic $firstname . ' ' . $lastname; when using in your template (content):

<?php printf( get_option( 'wc_custom_email_info' ), '{full_name}' ); ?>

<小时>

原答案

尝试以下使用 WC_Order 方法 get_formatted_billing_full_name():

Try the following instead that use WC_Order method 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 上没有定义;

It should works. Before on your code $firstname and $lastname were not defined;

这篇关于在 Woocommerce 自定义电子邮件内容上获取自定义电子邮件占位符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:根据购物车物品重量和购物车数量计算运费 下一篇:如何在 WooCommerce 订单完成页面中插入 Google Merchant Review JS 代码

相关文章

最新文章