如何在woocommerce中发送带有自定义内容的电子邮件以获取自定义订单状态

时间:2019-05-22 04:51:29

标签: php wordpress woocommerce wordpress-theming hook-woocommerce

我正在将WooCommerce用于我的网站。我需要添加自定义订单状态并在电子邮件中发送自定义内容以获取自定义状态。我使用以下代码创建了自己的订单状态,称为“已发货”,但是我的主要问题是,当我将订单状态更改为“已发货”时,电子邮件将通过完成的订单内容。如何在已发货订单状态中拥有自己的内容?

function register_brethren_order_statuses(){
    register_post_status( 'wc-shipped', array(
        'label' => 'Shipped',
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop( 'shipped <span class="count">(%s)</span>', 'Shipped <span class="count">(%s)</span>' )) );
} 

add_action( 'init', 'register_brethren_order_statuses' );


// Add Status to WooCommerce Status List
function add_brethren_order_statuses( $order_statuses ){
    $new_order_statuses = array();
    // Add New Status After Processing
    foreach ( $order_statuses as $key => $status ){
        $new_order_statuses[ $key ] = $status;
        if ( 'wc-processing' === $key ){
            $new_order_statuses['wc-shipped'] = 'Shipped';
        }
    }
    return $new_order_statuses;
}


add_filter( 'wc_order_statuses','add_brethren_order_statuses' );

add_action('woocommerce_order_status_shipped','bbloomer_status_custom_notification', 20, 2 );

function bbloomer_status_custom_notification( $order_id, $order ) {
    $heading = 'Shipped';
    $subject = 'Shipped';
    // Get WooCommerce email objects
    $mailer = WC()->mailer()->get_emails();

    $mailer['WC_Email_Customer_Completed_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->settings['heading'] = $heading;
    $mailer['WC_Email_Customer_Completed_Order']->subject = $subject;
    $mailer['WC_Email_Customer_Completed_Order']->settings['subject'] = $subject;

    // Send the email with custom heading & subject
    if (  $order->get_status() == "shipped" ) {
        $mailer['WC_Email_Customer_Completed_Order']->trigger( $order_id );
    }
}

add_action( 'woocommerce_email_before_order_table', 
'bbloomer_add_content_specific_email', 20, 4 );
function bbloomer_add_content_specific_email( $order, $sent_to_admin, 
$plain_text, $email ) {
    if (  $order->get_status() == "shipped" ) {
        echo '<h2 class="email-upsell-title">Shipped order</h2>Hi your order is shipping by post<p class="email-upsell-p"></p>';
    }
}

1 个答案:

答案 0 :(得分:0)

对于这个问题,似乎Customer Completed Order电子邮件正在执行。因此,有两种添加自己的自定义内容的方法。

1)通过将客户已完成的订单电子邮件临时模板从WooCommerce插件覆盖到主题文件夹,并根据要求进行必要的更改。

2)使用与电子邮件表格式相关的钩子。为此,请参见下面的代码段,它将在ID为customer_completed_order并且状态为shipped的电子邮件模板的表主体之前添加内容。

<?php
add_action( 'woocommerce_email_before_order_table', 'bbloomer_add_content_specific_email', 20, 4 );

function bbloomer_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
   if ( $email->id == 'customer_completed_order' && $order->get_status() == "shipped") { /*As customer completed order is getting executed and need to modify for the specific email*/
      echo '<h2 class="email-upsell-title">Lorem Ipsum sit Amet</p>';
   }
}
?>

您可以使用

查看的可能的电子邮件模板ID
<?php
if ( $email->id == 'cancelled_order' ) {}
if ( $email->id == 'customer_completed_order' ) {}
if ( $email->id == 'customer_invoice' ) {}
if ( $email->id == 'customer_new_account' ) {}
if ( $email->id == 'customer_note' ) {}
if ( $email->id == 'customer_on_hold_order' ) {}
if ( $email->id == 'customer_refunded_order' ) {}
if ( $email->id == 'customer_reset_password' ) {}
if ( $email->id == 'failed_order' ) {}
if ( $email->id == 'new_order' ) {}
?>

希望您可以玩这些东西并确保成功。