避免 Woocommerce 中特定产品类别的客户电子邮件通知

2023-12-12

在 Woocommerce 中,我试图阻止 woocommerce 中特定产品类别的客户订单电子邮件。

我尝试过的是:

add_filter('woocommerce_email_recipient_customer_processing_order', 'wc_change_customer_new_order_email_recipient', 10, 3);
function wc_change_customer_new_order_email_recipient( $recipient, $order ) {
 global $woocommerce;
    $items = $order->get_items();
    $category_ids = array( 10 );
    $flagHasProduct = false;
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        $terms = get_the_terms( $product_id, 'product_cat' ); 

        foreach ( $terms as $term ) {  
            if( in_array( $term->term_id, $category_ids ) ) {
                $flagHasProduct = true;
            }
        }

    }
    if ($flagHasProduct) {
        $recipient = "";
    } 
    $recipient = "";
    return $recipient;
}

但这个钩子根本不起作用。任何帮助表示赞赏。


你应该尝试这个(with has_term()WP 条件函数):

add_filter( 'woocommerce_email_recipient_customer_processing_order', 'product_cat_avoid_processing_email_notification', 10, 2 );
function product_cat_avoid_processing_email_notification( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    // HERE set your product categories (coma separated term Ids, slugs or names)
    $product_categories = array( 10 );

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get an instance of the WC_Product object
        $product = $item->get_product(); 
        // Get the correct product ID (for variations we take the parent product ID)
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

        // Check for product categories for this item
        if( has_term( $product_categories, 'product_cat', $product_id ) )
            return ''; // If it's found, we return an empty recipient
    }
    return $recipient;
}

此代码位于活动子主题(或主题)的 function.php 文件中。经过测试并有效。

这将避免仅客户“处理”订单电子邮件通知.

对于其他客户订单电子邮件通知,您还必须添加:

// Customer "completed" Order email notification
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'product_cat_avoid_processing_email_notification', 10, 2 );

// Customer "on-hold" Order email notification
add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'product_cat_avoid_processing_email_notification', 10, 2 );

// Customer "refunded" Order email notification
add_filter( 'woocommerce_email_recipient_customer_refunded_order', 'product_cat_avoid_processing_email_notification', 10, 2 );

// Customer "invoice" email notification
add_filter( 'woocommerce_email_recipient_customer_invoice', 'product_cat_avoid_processing_email_notification', 10, 2 );

  • 如何获取 WooCommerce 订单详细信息
  • 在 Woocommerce 3 中获取订单商品和 WC_Order_Item_Product
  • WordPress has_term() 函数(用于将自定义分类作为条件标签)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

避免 Woocommerce 中特定产品类别的客户电子邮件通知 的相关文章

  • 如何复制 ArrayIterator 以保留其当前迭代位置?

    因为这似乎是我必须做的才能达到这种效果 arr a gt first b gt second iter new ArrayIterator arr Do a bunch of iterations iter gt next new iter
  • SMTP 配置在生产中不起作用

    我正在尝试在提交表单时发送电子邮件 我正在使用 PHPMailer 使用以下配置发送邮件 mail new PHPMailer mail gt isSMTP mail gt Host mail example in mail gt Port
  • PHP 绑定“bigint”数据类型(MySQLi 准备好的语句)

    studentId 57004542323382 companyOfferId 7 sql INSERT INTO studentPlacement companyOfferId studentId VALUES if stmt db gt
  • PHP 开发相当于 Mongrel/Webrick 吗?

    PHP 开发中是否有与 Rails 开发期间使用 Mongrel Webrick 等效的方法 我通常在端口 3000 上使用 Mongrel 在开发过程中为我的 Rails 应用程序提供服务 我从事 PHP 开发已经有几年了 据我所知 方法
  • zip 文件的校验和

    我目前正在开发一个工具 它上传一组文件 然后使用 md5 校验和将文件与上一批上传的文件进行比较 并告诉您哪些文件已更改 对于常规文件 这工作正常 但某些上传的文件是 zip 存档 即使其中的文件相同 它们几乎总是会发生变化 有没有一种方法
  • 为什么这会返回资源 id #2? [复制]

    这个问题在这里已经有答案了 可能的重复 我如何从 PHP 中的 MySql 响应中 回显 资源 id 6 https stackoverflow com questions 4290108 how do i echo a resource
  • 如何通过php的require()或include()函数传递变量?

    当我使用这个时 require diggstyle code php page page no 警告是 无法打开流 第 198 行 C xampp htdocs 4ajax gallery core php 中没有错误 错误是 Failed
  • 为什么验证不起作用并跳转到另一个页面?

    我写了一个customer display php来验证数据 到目前为止只有名字 但无论名字字段是否为空 网页都会跳转到customer search php并且没有更改数据库中的信息 为什么
  • Magento - 检查 cms 页面

    我想通过 php 检查页面是否是 Magento 中的 cms page 我需要不同的 cms 页面面包屑 所以我尝试在一个条件下做到这一点 但我不知道如何或在哪里查看 到目前为止 这是我的 breadcrumbs phtml p some
  • 语法错误,第 288 行出现意外的“endif”(T_ENDIF)[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我一直在离线处理我的 WordPress 网站的此代码错误 解析错误 语法错误 homez 541 photoher marie
  • 为什么我的 if 语句没有按我预期的方式工作?

    我正在尝试实现以下目标 我向我的 SQL 数据库询问使用SELECT FROM subjects 这样做之后我要求使用数组mysqli fetch assoc 在那之前一切都很好 现在的问题是 当我尝试在每个循环中修改 genero 的值
  • 自动建议 php 的 ajax

    我有一个 html 表单 php 脚本和 jquery 我需要一个 ajax 代码来从我的 php 脚本中进行自动建议 以下是代码 表单 html
  • Joomla 页面中的自定义 php 代码

    我正在尝试将 Joomla 1 5 9 页面中的表源从页面中的硬编码 html 更改为从 SQL 数据库获取信息 执行此操作的正确方法是什么 创建一个新的模型或组件并以某种方式在页面中使用它 我找到了这些 http docs joomla
  • 如何在 OS X 上使用 OpenSSL 1.0.1 编译 PHP 5.5.19

    我已经安装了 OpenSSL 1 0 1j usr local ssl现在我尝试使用此版本的 OpenSSL 编译 PHP 5 5 19 这是我的配置过程 export CFLAGS arch x86 64 export CXXFLAGS
  • Sonata DateTimePickerType 类默认日期显示错误的日期时间格式

    我陷入困境 我不知道如何使用 sonata DateTimePickerType 类正确设置默认日期和时间 我尝试了不同的方法 但到目前为止 没有一种方法没有帮助 在下面的截图中 help 键显示正确的日期和时间 但是当我使用 dp 默认日
  • 如何在 Zend Framework 3 中注册自定义表单视图助手

    我正在将继承的 Zend Framework 2 应用程序迁移到 Zend Framework 3 并且在注册自定义表单视图助手时遇到了一些困难 这些助手在应用程序使用版本 2 时起作用 主要用于添加标签属性以实现可访问性 例如 这是一个自
  • 如何使用 PHP 对字符串进行 rot13 处理?

    我有一个很大的 php 代码 我想手动对其进行编码和解码 我的问题是 php 代码里面有很多单引号和双引号 因此我在使用时出现错误str rot13 功能如下 那么正确的语法是什么以及如何使用下面的函数进行编码 str rot13 That
  • 从 PHP 数组生成 HTML 表

    我不明白这一点 我需要解决看似简单的问题 但这超出了我的逻辑 我需要编写一个函数 table columns input cols 它将输出一个表 示例 input array apple orange monkey potato chee
  • 检查 $_POST 数据

    我正在对表单进行一些垃圾邮件检查 下面的代码在我的本地主机上正常工作 如果为 true 则重定向到 google com 但是 当它在生产服务器上时却不起作用 执行脚本的其余部分并且不重定向到 Google com if POST SERV
  • php curl 使用 GET 发送变量 奇怪的结果

    我正在尝试调用远程站点上页面中的网址 决定使用curl 在远程站点上 url 变量显示为 REQUEST Array var1 gt val1 amp var2 gt val2 amp var3 gt val3 被调用的url是 http

随机推荐