仅在Woocommerce中针对本地运输和已完成订单的自定义电子邮件通知

时间:2018-09-13 14:04:22

标签: php wordpress woocommerce email-notifications shipping-method

我在上一个线程中发现了this代码(确实有效),该代码使自定义消息可以通过电子邮件发送给选择了本地取货方式作为运送方式的客户。

add_action( 'woocommerce_email_order_details', 
'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, 
$sent_to_admin, $plain_text, $email ) {
// Only for processing and completed email notifications to customer
if( ! ( 'customer_processing_order' == $email->id || 
'customer_completed_order' == $email->id ) ) return;

foreach( $order->get_items('shipping') as $shipping_item ){
    $shipping_rate_id = $shipping_item->get_method_id();
    $method_array = explode(':', $shipping_rate_id );
    $shipping_method_id = reset($method_array);
    // Display a custom text for local pickup shipping method only
    if( 'local_pickup' == $shipping_method_id ){
        echo '<p><strong>Ritiro in sede</strong></p>';
        break;
    }
}

我的问题是:我如何调整代码以删除处理订单电子邮件,以便仅处理已完成的订单电子邮件?

1 个答案:

答案 0 :(得分:1)

您只需要在函数代码中替换如下:

// Only for processing and completed email notifications to customer
if( ! ( 'customer_processing_order' == $email->id || 
'customer_completed_order' == $email->id ) ) return;

由此:

// Only for completed email notifications to customer
if( 'customer_completed_order' != $email->id ) return;

它仅适用于向客户“完成”订单通知…因此,完整的代码:

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for completed email notifications to customer
    if( 'customer_completed_order' != $email->id ) return;

    foreach( $order->get_items('shipping') as $shipping_item ){
        $shipping_rate_id = $shipping_item->get_method_id();
        $method_array = explode(':', $shipping_rate_id );
        $shipping_method_id = reset($method_array);
        // Display a custom text for local pickup shipping method only
        if( 'local_pickup' == $shipping_method_id ){
            echo '<p><strong>Ritiro in sede</strong></p>';
            break;
        }
    }
}

相关:Add a custom text to specific email notification for local pickup Woocommerce orders