两天后为 WooCommerce 暂停订单发送自定义提醒电子邮件 [英] Send a custom reminder email for WooCommerce On-Hold orders after two days

查看:59
本文介绍了两天后为 WooCommerce 暂停订单发送自定义提醒电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在订单状态为暂停且订单创建时间为 48 小时或更长时间的情况下向客户发送一封包含自定义文本的电子邮件.

My goal is to send an email to the customer containing custom text if the order status is on-hold and if the order creation time is 48 hours or more old.

  1. 订单已存在 48 小时或更长时间
  2. 向客户发送电子邮件
  3. 要求客户付款
  4. 包含订单链接(到我的帐户付款页面)

我正在尝试使用 回答我之前关于在 WooCommerce 中没有付款的情况下在 X 天后自动取消订单的问题之一.

I'm trying to use the code from an answer to one of my previous questions about Automatically cancel order after X days if no payment in WooCommerce.

我稍微更改了代码:

add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
    global $pagenow, $post_type;

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

    $days_delay = 5;

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

    $unpaid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );

    if ( sizeof($unpaid_orders) > 0 ) {
        $reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");

        foreach ( $unpaid_orders as $order ) {
            // HERE I want the email to be sent instead  <===  <===  <===  <===  <=== 
        }
    }
    update_option( 'unpaid_orders_daily_process', $today + $one_day );

    endif;
}

这是我想与上面同步的电子邮件部分(阅读代码注释):

This is the email part that I want to sync with the above (read the code comments):

add_action ('woocommerce_email_order_details', 'on_hold_payment_reminder', 5, 4);
function on_hold_payment_reminder( $order, $sent_to_admin, $plain_text, $email ){

    if ( 'customer_on_hold_order' == $email->id ){
        $order_id = $order->get_id();

        echo "<h2>Do not forget about your order..</h2>
        <p>CUSTOM MESSAGE HERE</p>";
    }
}

那么,如何使用自定义文本为暂停"订单发送电子邮件通知提醒?

So how can I send an email notification reminder for "on-hold" orders with a custom text?

推荐答案

以下代码将每天触发一次,并将发送一封电子邮件提醒,其中包含未付款订单的自定义消息(对于暂停"订单状态):

The following code will be triggered once daily and will send an email reminder with a custom message on unpaid orders (for "on-hold" order status):

add_action( 'restrict_manage_posts', 'on_hold_payment_reminder' );
function on_hold_payment_reminder() {
    global $pagenow, $post_type;

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

    $days_delay = 2; // 48 hours
    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );

    $unpaid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );

    if ( sizeof($unpaid_orders) > 0 ) {
        $reminder_text = __("Payment reminder email sent to customer $today.", "woocommerce");

        foreach ( $unpaid_orders as $order ) {
            $order->update_meta_data( '_send_on_hold', true );
            $order->update_status( 'reminder', $reminder_text );

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

    endif;
}


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

        echo '<h2>'.__("Do not forget about your order.").'</h2>
        <p>'.sprintf( __("CUSTOM MESSAGE HERE… %s"), 
            '<a href="'.$order_link.'">'.__("Your My account order #").$order_number.'<a>'
        ) .'</p>';

        $order->delete_meta_data('_send_on_hold');
        $order->save();
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

这篇关于两天后为 WooCommerce 暂停订单发送自定义提醒电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆