两天后发送有关WooCommerce保留订单的自定义提醒电子邮件

时间:2019-04-23 11:03:11

标签: php wordpress woocommerce orders email-notifications

我的目标是,如果订单状态为保留状态并且订单创建时间为48小时或更早,则向客户发送包含自定义文本的电子邮件。

  1. 订单已超过48小时
  2. 发送电子邮件给客户
  3. 要求客户付款
  4. 包括订单的链接(到我的帐户付款页面)

我正在尝试使用an answer中的代码来解决以前的问题之一,即如果WooCommerce中没有付款,则X天后自动取消订单。

我轻轻更改了代码:

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

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

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

那么我如何发送带有自定义文本的“保留”订单的电子邮件通知提醒?

1 个答案:

答案 0 :(得分:0)

以下代码将每天触发一次,并将针对未付订单(用于“保留”订单状态)发送电子邮件提醒和自定义消息:

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文件中。经过测试,可以正常工作。