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

标签 php wordpress woocommerce metadata email-notifications

如果特定元具有特定值(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。

引用资料:
Get custom order item metadata in Woocommerce 3
How to get WooCommerce order details
WooCommerce: Show notice on new order email if specific payment method is used

最佳答案

我已经测试了你的代码,它适用于一个注册的订单项目元数据,它的键是 Umbrella Hole,请在下面的 wp_woocommerce_order_itemmeta 表中查看 line_item :

enter image description here

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:

enter image description here

关于php - 如果 Woocommerce 订单项具有特定的自定义元数据,则向电子邮件添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55010149/

相关文章:

php - 使用 WooCommerce 选择变体时更新产品页面上的价格

php - 在 Woocommerce 单个产品页面和购物车上有条件地设置特定产品价格

PHP 调整图像大小并保存透明 PNG

php - 如何在 WooCommerce 管理订单列表上创建过滤器下拉菜单?

PHP PDO 动态查询构建

c# - wordpress 最好的 C# 代码高亮插件是什么?

php - Silverstripe 3.2 可重用 block

php - 来自 Yoast on wordpress 的 Echo 页面描述

css - style.css wordpress 模板菜单对齐

css - 如果指定了任何类,WooCommerce 产品简短描述将呈现两次