Get custom email placeholder value on Woocommerce custom email content

笑着哭i 提交于 2019-12-12 21:53:06

问题


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();
}

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

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

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

But I need this here:

Blaaaaaaa Joe Martin blaaaa

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.


回答1:


Updated

Placeholders in email are only for the email subject in Woocommerce

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

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

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}' ); ?>

Original answer

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();
}

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



来源:https://stackoverflow.com/questions/53438921/get-custom-email-placeholder-value-on-woocommerce-custom-email-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!