[WooCommerce] 使用 WP Cron 方法提醒未付款訂單記得付款

本篇文章更新時間:2020/04/13
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~
如果本站內容對你有幫助,歡迎使用 BFX Pay 加密貨幣新台幣 贊助支持。


實作方式不難,就是紀錄一下方法!市面上比較常出現的需求是「abandoned cart」,追還在購物車裡沒完成下單的追下單功能。

本篇是用在客製化通知追訂單付款。

WooCommerce 有人下單後 如有開啟庫存管理會導致商品數量被綁住,所以通常會有一個付款期限,透過取消訂單來恢復庫存,免得有人想下單還買不到貨。不過如果寬限客戶有較長時間可以付款的話,就需要特別來提醒記得付款了!

針對 WP Cron 機制的了解可以參考這篇: [WordPress] 詳解定時定期運作的服務: WP-Cron

function mxp_add_wc_cron_schedules($schedules) {
    $schedules['ks_check_order_1h'] = array(
        'interval' => 3600, // 一小時檢查一次變化
        'display'  => "一小時一次",
    );
    return $schedules;
}
add_filter('cron_schedules', 'mxp_add_wc_cron_schedules');

function mxp_cronjob_register_for_wc() {
    if (!wp_next_scheduled('mxp_wc_checker_cron')) {
        wp_schedule_event(time(), 'ks_check_order_1h', 'mxp_wc_checker_cron');
    }
}
add_action('wp', 'mxp_cronjob_register_for_wc');

function mxp_wc_cronjob_do_action() {
    global $wpdb;
    $days_delay    = 7 * DAY_IN_SECONDS; // 7天
    $today         = time();
    $unpaid_orders = (array) wc_get_orders(array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ($today - $days_delay),
    ));
    foreach ($unpaid_orders as $key => $order) {
        $id            = $order->get_id();
        $first_name    = $order->get_billing_first_name();
        $last_name     = $order->get_billing_last_name();
        $order_number  = $order->get_order_number();
        $created_at    = $order->get_date_created()->date('Y-m-d H:i:s');
        $created_at_ts = strtotime($created_at);
        $email         = $order->get_billing_email();
        $order_url     = $order->get_view_order_url();
        $count         = get_post_meta($id, 'wc_order_notify_count', true);
        $last_notify   = get_post_meta($id, 'wc_order_last_notify_time', true);
        if (empty($count)) {
            $count = 0;
        }
        if (empty($last_notify)) {
            $last_notify = $created_at_ts;
        }
        if ($count < 3 && ($today - $last_notify) > $days_delay) {
            $subject = "[一介資男] 付款提醒,訂單編號 #{$order_number}";
            $body    = "您好,{$first_name}{$last_name},您有一筆於 {$created_at} 建立且尚未付款的訂單:{$order_url}

付款資訊:
方式:銀行匯款
銀行:OOOOOOOOOO
戶名:OOOO有限公司
帳號:1234432112345"; wp_mail($email, $subject, $body, array('Content-Type: text/html; charset=UTF-8')); $count += 1; update_post_meta($id, 'wc_order_notify_count', $count); update_post_meta($id, 'wc_order_last_notify_time', $today); } } } add_action('mxp_wc_checker_cron', 'mxp_wc_cronjob_do_action');

程式裡設定每小時去檢查一次是否有需要通知,最多通知到三次,每次檢查通知間隔是否超過一周,避免太頻繁的通知。至於通知方法就寫個發信功能來處理,還有其他方法都可以在那程式裡去調整。


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[///////////____36%_________]

參與討論

8 則留言

  1. 專屬於 WooCommerce 的排程工作,會建議改用 Action Scheduler ( WooCommerce 內鍵的另外一套排程系統 ) 來執行相關的動作。

  2. Alex Lion 噗 我都沒動耶! 真的搞不清楚它的機制

  3. 哭哭,我記得也不能手動加入。

    開放手動加入的話,記得幫我加一下 XDD

發佈留言

發佈回覆給「Alex Lion」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


文章
Filter
Apply Filters
Mastodon