如果 Woocommerce 订单项具有特定的自定义元数据,则向电子邮件添加文本

Add a text to emails if Woocommerce order item has a specific custom metadata

如果特定元具有特定值(Umbrella Hole 是 "YES"),我想在 订购发送给管理员的电子邮件中添加一个通知部分

到目前为止的代码:

function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    foreach( $order->get_items() as $item ){
        $target_value = $item->get_meta('Umbrella Hole');
        if ($target_value == "Yes") {
            echo '<div style="background-color:antiquewhite;padding:5px;margin-bottom:10px;"><strong><span style="color:red;">Note:</span></strong> Umbrella Hole is present in the order. Please make sure velcro zipper split is requested from supplier too.</div>';
        }
    }
}
add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );

但它不起作用。我做错了什么吗?使用最新版本的 WordPress 和 WooCommerce。

参考文献:


WooCommerce: Show notice on new order email if specific payment method is used

我已经测试了您的代码,它适用于键为 Umbrella Hole 的已注册订单项元数据,请参阅下面的 wp_woocommerce_order_itemmeta table 以获得 line_item :

So the problem can only comes from the order item custom meta data that is not registered

我重新审视了你的代码:

add_action( 'woocommerce_email_order_details', 'add_order_instruction_email', 10, 4 );
function add_order_instruction_email( $order, $sent_to_admin, $plain_text, $email ) {
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if ( "Yes" == $item->get_meta('Umbrella Hole') ) {
            echo '<div style="background-color:antiquewhite;padding:5px;margin-bottom:10px;"><strong><span style="color:red;">Note:</span></strong> Umbrella Hole is present in the order. Please make sure velcro zipper split is requested from supplier too.</div>';
            $break; // Stop the loop to avoid repetitions
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

Here below the email notification when any order line item has a registered meta data Umbrella Hole with "yes" as value: