在 X 天 WooCommerce 订单后发送自定义电子邮件 [重复]

Posted

技术标签:

【中文标题】在 X 天 WooCommerce 订单后发送自定义电子邮件 [重复]【英文标题】:Send custom e-mail after X days WooCommerce order [duplicate] 【发布时间】:2019-08-23 22:31:30 【问题描述】:

如果“处理订单”超过五天,我正在尝试创建一个应该发送自定义电子邮件的函数。

我有点卡在我的代码上。它似乎不起作用 - 什么都没有发生。我还想知道如何将订单 ID 添加到自定义电子邮件的正文中?

我的代码:

// Lookup DB for orderdate older than 5 days AND send E-mail
function expire_after_x_days()
        global $wpdb;
    // Get current time
        $today = date("mdy");

    // set time to expire
        $time_to_expire = "-5 days";
        $expiration_date = date("mdy", strtotime( $today . $time_to_expire));

    // Get orders with processing status
        $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-processing'");

        if( !empty($result)) foreach ($result as $order)
            // Get order's time
            $order_time = get_the_time('mdy', $order->ID );

    // Compare order's time with current time
        if ( $order_time < $expiration_date )

    // send custom email   
        $to = 'test@gmail.com';
        $subject = 'Test subject of my email';
        $body = 'The email body content. Perhaps also write order ID';
        $headers = array('Content-Type: text/html; charset=UTF-8');

wp_mail( $to, $subject, $body, $headers );
            
        
 
    add_action( 'admin_footer', 'expire_after_x_days' );

【问题讨论】:

或许this或this能给你一些启发。 谢谢你,洛伊克!我会看看你的链接,看看我是否可以将电子邮件部分放入我的代码中:) 【参考方案1】:

灵感来自Send a custom reminder email for WooCommerce On-Hold orders after two days 回答代码,以下重新访问的代码将每天触发一次,并会在 5 天后发送一封后续电子邮件,其中包含有关处理订单的自定义消息(用于“处理”订单状态):

/*Add follow up email*/
add_action( 'restrict_manage_posts', 'follow_up_email_processing_order' );
function follow_up_email_processing_order() 
    global $pagenow, $post_type;

    if( 'shop_order' === $post_type && 'edit.php' === $pagenow
        && get_option( 'processing_orders_followup_daily_process' ) < time() ) :

    $days_delay = 5; // 5 Days
    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );

    $processing_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'processing',
        'date_modified' => '<' . ( $today - ($days_delay * $one_day) ), //Get modified date to know how many time has passed since it changed its status to processed
    ) );

    if ( sizeof($processing_orders) > 0 ) 
        $reminder_text = __("Followup email sent $today.", "woocommerce");

        foreach ( $processing_orders as $order ) 
            $order->update_meta_data( '_send_processing', true );
            $order->update_status( 'follow-up', $reminder_text );

            $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
            $wc_emails['WC_Email_Customer_Processing_Order']->trigger( $order->get_id() ); // Send email
        
    
    update_option( 'processing_orders_followup_daily_process', $today + $one_day );

    endif;



add_action ( 'woocommerce_email_order_details', 'processing_followup_notification', 5, 4 );
function processing_followup_notification( $order, $sent_to_admin, $plain_text, $email )
    if ( 'customer_processing_order' == $email->id && $order->get_meta('_send_processing') )
        $order_id     = $order->get_id();
        //$order_link   = wc_get_page_permalink('myaccount').'view-order/'.$order_id.'/';
        $order_link = $order->get_checkout_order_received_url();
        $order_number = $order->get_order_number();

        echo '<h2>'.__("We haven't forgoten about you!.").'</h2>
        <p>'.sprintf( __("CUSTOM MESSAGE HERE… %s"), 
            '<a href="'.$order_link.'">'.__("Your My account order #").$order_number.'<a>'
        ) .'</p>';

        $order->delete_meta_data('_send_processing');
        $order->save();
    


/* Modify the subject */
add_filter('woocommerce_email_subject_customer_processing_order', 'change_followup_email_subject', 1, 2);
function change_followup_email_subject( $subject, $order ) 
    global $woocommerce;

    $first_name = strtok( $order->billing_first_name,  ' ' );

    $subject = sprintf( '¡%s Your order is still being processed!', $first_name );

    return $subject;

【讨论】:

而不是买方,而是将电子邮件发送到特定的电子邮件。如何做到这一点? 您的代码当前设置为工作日。如何设置为工作日(不包括周末)?

以上是关于在 X 天 WooCommerce 订单后发送自定义电子邮件 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Woocommerce:结帐页面后获取订单ID [重复]

在 WooCommerce 中自动处理已付款订单而不是自动完成

PayPal EC 付款完成后,woocommerce 中的订单状态如何更改? [关闭]

复制/复制订单 Woocommerce

过滤 Woocommerce 订单发送的 PayPal“自定义”字段

在 Woocommerce 中仅向 PayPal 发送订单号而不是商品名称