0

我在之前的线程中发现了这个代码(它确实有效),它可以为选择本地取货作为送货方式的客户发送电子邮件的自定义消息。

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

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

4

1 回答 1

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

相关:在本地取货 Woocommerce 订单的特定电子邮件通知中添加自定义文本

于 2018-09-13T15:20:32.077 回答