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

问题描述 投票:3回答:1

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

php wordpress woocommerce metadata email-notifications
1个回答
2
投票

我已经测试了你的代码,它适用于已注册的订单项元数据,其关键是Umbrella Hole,请参阅wp_woocommerce_order_itemmeta表格中的line_item

enter image description here

所以问题只能来自未注册的订单商品自定义元数据

我轻轻地重温了你的代码:

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文件中。经过测试和工作。

如果任何订单行项目具有Umbrella Hole作为值的已注册元数据"yes",则在下面的电子邮件通知中:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.