如果特定元数据具有特定值(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
:
所以问题只能来自未注册的订单商品自定义元数据
我轻轻地重温了你的代码:
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"
,则在下面的电子邮件通知中: