簡體   English   中英

避免WooCommerce Dokan中父訂單的訂單完成通知

[英]Avoid order completed notification for parent orders in WooCommerce Dokan

我對下面的代碼有疑問。 我們使用此處的代碼: Dokan 插件為單個訂單的客戶發送多個 email 以確保系統不會向客戶發送訂單處理郵件以進行子訂單,因此僅針對“父”訂單。 (這完全有效!=> 下面代碼中的第 1 部分)

問題:但是,如果該訂單有子訂單,我們還必須確保不會為父訂單發送訂單完成郵件。 為什么? 因為每個子訂單完成后,父訂單也會自動標記為已完成。 如果此父訂單已完成,我們不想發送該郵件。

為此,我們更改了鈎子以及 IF 語句。 但目前,訂單完成郵件也會在所有子訂單完成后發送。 (下面代碼中的第 2 部分)

據我了解,我必須檢查訂單是否為父訂單,如果是,請不要發送訂單已完成的郵件。 if (get_post_meta( $order->get_id(), 'has_sub_order', true ) ){ return ''; }

更新:我認為它不起作用,因為使用了鈎子woocommerce_email_recipient_customer_completed_order 對於處理郵件,我使用woocommerce_email_recipient_customer_processing_order並且可以。

// PART 1
// Do not send order processing mail for sub orders
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'conditional_email_notification_order_processing', 10, 2 );
function conditional_email_notification_order_processing( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    if (!get_post_meta( $order->get_id(), 'has_sub_order', true ) && !empty(wp_get_post_parent_id($order->get_id())) ){
        return '';
    }
    return $recipient;
}

// PART 2
// Do not send order completed mail for parent order (IF SUB ORDERS = TRUE)
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'conditional_email_notification_order_completed', 10, 2 );
function conditional_email_notification_order_completed( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    if ( get_post_meta( $order->get_id(), 'has_sub_order', true ) && empty(wp_get_post_parent_id($order->get_id())) ){
        return '';
    }
    return $recipient;
}

您的代碼中的主要錯誤是empty(wp_get_post_parent_id($order->get_id())在您的第二個代碼片段中,當它是具有 suborders 的主訂單時,它不為空。在這種情況下,父訂單 ID 的值為0 (所以不為空)

嘗試以下重新訪問的代碼:

// Avoid customer processing order notification for children orders
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'filter_email_recipient_customer_processing_order', 10, 2 );
function filter_email_recipient_customer_processing_order( $recipient, $order ) {
    if ( is_a( $order, 'WC_Order' ) ) {
        $has_sub_order = $order->get_meta('has_sub_order');
        $parent_order  = $order->get_parent_id();
        
        if ( ! $has_sub_order && $parent_order > 0 ){
            return '';
        }
    }
    return $recipient;
}


// Avoid customer completed order notification for main parent order
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'filter_email_recipient_customer_completed_order', 10, 2 );
function filter_email_recipient_customer_completed_order( $recipient, $order ) {
    if ( is_a( $order, 'WC_Order' ) ) {
        $has_sub_order = $order->get_meta('has_sub_order');
        $parent_order  = $order->get_parent_id();
        
        if ( $has_sub_order && $parent_order == 0 ){
            return '';
        }
    }
    return $recipient;
}

代碼進入活動子主題(或活動主題)的functions.php文件。 它可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM