Php 如何在woocommerce中发送包含自定义订单状态的自定义内容的电子邮件

Php 如何在woocommerce中发送包含自定义订单状态的自定义内容的电子邮件,php,wordpress,woocommerce,wordpress-theming,hook-woocommerce,Php,Wordpress,Woocommerce,Wordpress Theming,Hook Woocommerce,我正在使用WooCommerce作为我的网站。我需要添加自定义订单状态并在电子邮件中发送自定义内容以获得自定义状态。我使用了以下代码并创建了自己的订单状态,名为Shipped,但我的主要问题是,当我将订单状态更改为Shipped时,会发送包含完整订单内容的电子邮件。我怎样才能使自己的内容处于已发货订单状态 function register_brethren_order_statuses(){ register_post_status( 'wc-shipped', array(

我正在使用WooCommerce作为我的网站。我需要添加自定义订单状态并在电子邮件中发送自定义内容以获得自定义状态。我使用了以下代码并创建了自己的订单状态,名为Shipped,但我的主要问题是,当我将订单状态更改为Shipped时,会发送包含完整订单内容的电子邮件。我怎样才能使自己的内容处于已发货订单状态

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>';
    }
}
功能寄存器\u顺序\u状态(){
注册后状态('wc shipped',数组(
“标签”=>“已装运”,
“public”=>正确,
“从搜索中排除”=>false,
“在\u管理\u所有\u列表中显示\u”=>true,
“在管理状态列表中显示”=>true,
“标签计数”=>(已装运(%s),“已装运(%s));
} 
添加操作('init','register\u order\u status');
//将状态添加到状态列表中
功能添加订单状态($order\U STATUS){
$new_order_status=array();
//处理后添加新状态
foreach($key=>$status的订单状态){
$new_order_status[$key]=$status;
if('wc processing'==$key){
$new_order_status['wc-shipped']='shipped';
}
}
返回$new\u order\u状态;
}
添加过滤器(“wc订单状态”、“添加订单状态”);
添加行动(“商业、订单、状态、发货”、“bbloomer、状态、定制、通知”,20、2);
功能bbloomer\u status\u custom\u notification($order\u id,$order){
$标题='已装运';
$subject='Shipped';
//获取电子邮件对象
$mailer=WC()->mailer()->get_emails();
$mailer['WC\u Email\u Customer\u Completed\u Order']->heading=$heading;
$mailer['WC\u Email\u Customer\u Completed\u Order']->设置['heading']=$heading;
$mailer['WC\u Email\u Customer\u Completed\u Order']->subject=$subject;
$mailer['WC\u Email\u Customer\u Completed\u Order']->设置['subject']=$subject;
//发送带有自定义标题和主题的电子邮件
如果($order->get_status()=“shipped”){
$mailer['WC\u Email\u Customer\u Completed\u Order']->触发器($Order\u id);
}
}
添加操作('woocommerce\u email\u在订单表之前',
“bbloomer\u添加内容\u特定电子邮件”,20,4);
功能bbloomer\u添加\u内容\u特定\u电子邮件($order,$sent\u至\u管理员),
$plain_text$email){
如果($order->get_status()=“shipped”){
echo“发货订单”您的订单是通过邮局发货的

; } }
对于这个问题,
客户完成的订单
电子邮件似乎正在执行。因此,有两种方法可以添加您自己的自定义内容

1) 通过覆盖客户完成的订单电子邮件tempalte,将其从WooCommerce插件发送到主题文件夹,并根据需要进行必要的更改

2) 使用与电子邮件表格式相关的挂钩。有关此信息,请参见下面的代码段,它将在id为
customer\u completed\u 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' ) {}
?>

希望你能玩这些东西,并获得成功