Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

php - Send a custom reminder email for WooCommerce On-Hold orders after two days

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. order is 48 hours old or more
  2. send email to customer
  3. ask customer to pay
  4. include a link to the order (to my account payment page)

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.

I have lightly changes the code:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...