简体   繁体   English

添加文本到 WooCommerce 客户完成订单 email 基于运输和订单备注

[英]Add a text to WooCommerce customer complete order email based on shipping and order note

I am trying to, based on shipping and if / if not the order note is empty, to add information to the customer complete order email.我正在尝试根据运输和如果/如果不是订单注释为空,将信息添加到客户完成订单 email。

Two different messages based on if the order notes field is filled in or not.基于是否填写订单备注字段的两条不同消息。 I've placed test orders but nothing shows up.我已经下了测试订单,但没有任何显示。

This is the code I am trying to get to work:这是我试图开始工作的代码:

add_action( 'woocommerce_email_order_details', 'local_pickup_order_instructions', 10, 4 );
function local_pickup_order_instructions( $order, $sent_to_admin, $plain_text, $email ) {

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

    if ('local_pickup' == $shipping_method_id && empty($_POST['order_comments'])){ ?>

        <div style="">Your instructions text here</div>

    <?php

    break;
    }
        else {

    if ('local_pickup' == $shipping_method_id && !empty($_POST['order_comments'] ) ) { ?>

        <div style="">Your instructions text here</div>
    
        <?php

        }
    }
}
}

There are some mistakes in your code… To display a different custom text when shipping method is "Local Pickup" if there is (or not) a customer note, use the following simplified and revisited code:您的代码中存在一些错误...如果有(或没有)客户备注,当运输方式为“本地取货”时显示不同的自定义文本,请使用以下简化和重新访问的代码:

add_action( 'woocommerce_email_order_details', 'local_pickup_order_instructions', 10, 4 );
function local_pickup_order_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    if ( $email->id === 'customer_completed_order' ) {
        $shipping_items = $order->get_items('shipping');
        $shipping_item  = reset($shipping_items); // Get first shipping item
        $customer_note  = $order->get_customer_note(); // Get customer note

        // Targeting Local pickup shipping methods
        if ( strpos( $shipping_item->get_method_id(), 'local_pickup' ) !== false ) {
            if ( empty($customer_note) ) {
                echo '<div style="color:red;">'.__("Instructions text here… (NO costumer note)").'</div>'; // Empty order note
            } else {
                echo '<div style="color:green;">'.__("Instructions text here… (has a costumer note)").'</div>'; // Filled order note
            }
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM