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