编程语言
首页 > 编程语言> > php-如果Woocommerce订单项具有特定的自定义元数据,则向电子邮件中添加文本

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

作者:互联网

如果特定的meta具有特定的值(“伞孔”为“是”),我想添加一个通知部分来订购发送给管理员的电子邮件.

到目前为止的代码:

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:

07001

标签:woocommerce,metadata,email-notifications,wordpress,php
来源: https://codeday.me/bug/20191211/2105894.html