ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-12-11 03:33:40  阅读:364  来源: 互联网

标签:woocommerce metadata email-notifications wordpress php


如果特定的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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有